2011-11-06 73 views
-1

我幾天前有類似的problem,但現在更復雜。使用ArrayList從不同的類創建類的新實例

我想從窗體2中的窗體1中使用ArrayList。我無法創建窗體2的新實例,因爲它會使我的窗體2的內容爲空。我怎麼能通過改變我的代碼來做到這一點?

舉例說明假人。

編輯

 int totalEntries = 0; 

     var myarr = SharedResources.Instance.ArrayList; 


     if(cmbType.SelectedIndex == 0) 
      myarr.Add(new AddPC(cmbType.Text, 
       txtUserName.Text, txtPassword.Text)); 

爲什麼上面的代碼引起的NullReferenceException?

private void sfdSaveToLocation_FileOk(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    EncryptDecrypt en = new EncryptDecrypt(); 

    StringBuilder sb = new StringBuilder(); 

    // I need the arraylist here but I can not access it as it is. 
    foreach (var list in addWindow.addedEntry) 
    { 
     if (list is AddPC) 
     { 
      AddPC tmp = (AddPC)list; 
      sb.Append(tmp.ToString()); 
     } 
     else if (list is AddWebSite) 
     { 
      AddWebSite tmp = (AddWebSite)list; 
      sb.Append(tmp.ToString()); 
     } 
     else if (list is AddSerialCode) 
     { 
      AddSerialCode tmp = (AddSerialCode)list; 
      sb.Append(tmp.ToString()); 
     } 
    } 

    File.WriteAllText(sfdSaveFile.FileName, sb.ToString()); 
} 

我已經在這裏AddEntryWindow形式的新實例:

private void tsmiAddEntry_Click(object sender, EventArgs e) 
     { 
      if (storedAuth == null) 
      { 
       DialogResult result = MessageBox.Show 
        ("You must log in before you add an entry." 
        + Environment.NewLine + "You want to authenticate?", 
        "Information", MessageBoxButtons.YesNo, 
        MessageBoxIcon.Information); 

       if (result == DialogResult.Yes) 
       { 
        AuthenticationWindow authWindow = 
         new AuthenticationWindow(); 
        authWindow.ShowDialog(); 
        storedAuth = authWindow.Result; 

        AddEntryWindow addWindow = new AddEntryWindow 
         (this, storedAuth.UserName, storedAuth.Password); 
        addWindow.ShowDialog(); 
       } 
       else 
       { 

       } 
      } 
      else 
      { 
       AddEntryWindow addWindow = new AddEntryWindow 
        (this, storedAuth.UserName, storedAuth.Password); 
       addWindow.ShowDialog(); 
      } 
     } 

問候。

+1

你爲什麼要使用ArrayList的' '?除非你使用.NET 1.1,否則你應該使用其中一個通用集合。 –

+0

@John Saunders:通過使用列表將解決我的問題? – HelpNeeder

回答

2

的思路可以創建只能有 數組列表和共享一個實例第三singletone類就那麼在您的應用程序的每個類都可以使用它

public class ShareArray 
{ 
    private System.Collections.ArrayList arrayList; 

    #region Property 
    public System.Collections.ArrayList ArrayList { get{return arrayList;}} 
    #endregion 

    #region Imp. signletone 
    private static ShareArray instance; 
    public static ShareArray Instance 
    { 
     get 
     { 
      if (instance == null) 
      { 
       instance = new ShareArray(); 
      } 
      return instance; 
     } 
    } 


    private ShareArray() 
    { 
     arrayList = new System.Collections.ArrayList(); 
    } 
    #endregion 
} 

,並使用這個類的每一個地方,你在想這樣

ShareArray.Instance.ArrayList.Add(value); 

var myarr = ShareArray.Instance.ArrayList; 
+0

我曾經見過人們在談論單身人士,但我並沒有完全理解它是如何運作的。很好的例子!我會看看它。 – HelpNeeder

+2

基本的想法是隻有一個類的應用程序也應用我也研究有關DesignPatterns http://www.dofactory.com/Patterns/Patterns.aspx – DeveloperX

+0

我有一些異常拋出。你能查看我的編輯嗎? – HelpNeeder

-1

一種解決方法是將數組列表定義爲Static。但這不是Good Choose

描述更好的程序設計! Form 1打開Form 2嗎?