我幾天前有類似的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();
}
}
問候。
你爲什麼要使用ArrayList的' '?除非你使用.NET 1.1,否則你應該使用其中一個通用集合。 –
@John Saunders:通過使用列表將解決我的問題? –
HelpNeeder