使用来自不同类的ArrayList而不创建类的新实例

时间:2011-11-06 05:35:42

标签: c# arraylist parameter-passing

几天前我有类似的problem但现在更复杂了。

我试图在表单1中使用表单2中的ArrayList。我无法创建表单2的新实例,因为它将使表单2的内容为null。我怎么能通过改变我的代码来做到这一点?

推荐假人的例子。

修改

        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();
            }
        }

问候。

2 个答案:

答案 0 :(得分:2)

一个想法可以创建第三个单一类,只能有一个实例 数组列表并共享它,以便您的应用程序中的每个类都可以使用它

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;

答案 1 :(得分:-1)

一种解决方案是将数组列表定义为Static。但这不是Good Choose

更好地描述您的程序设计!表格1是开放形式2吗?

相关问题