C# 尝试从arraylist中的结构检索数据时发生C ArgumentOutOfRange异常
我将尝试只发布相关代码,因为我的程序已经相当大了。基本上,程序会将客户信息添加到arraylist结构中。我已经让存储、保存和文件加载工作完美无瑕,但当我试图显示数据时,我遇到了异常 大多数主代码都在与表单分离的类上,这个特定的调用来自frmViewRecordsC# 尝试从arraylist中的结构检索数据时发生C ArgumentOutOfRange异常,c#,arraylist,structure,C#,Arraylist,Structure,我将尝试只发布相关代码,因为我的程序已经相当大了。基本上,程序会将客户信息添加到arraylist结构中。我已经让存储、保存和文件加载工作完美无瑕,但当我试图显示数据时,我遇到了异常 大多数主代码都在与表单分离的类上,这个特定的调用来自frmViewRecords public void ViewData(int currentRecord) { string fn = ((custDetails)datalist[currentRecord]).firstName;
public void ViewData(int currentRecord)
{
string fn = ((custDetails)datalist[currentRecord]).firstName;
frmViewRecords viewRecords = new frmViewRecords();
viewRecords.WriteData(fn);
}
上面的代码是导致异常的原因,但是下面messagebox的代码工作正常
public void LoadData()
{
bool fileLoaded = false;
//Load the database
try
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //Create the filestream
try
{
BinaryFormatter bf = new BinaryFormatter(); //New binary formatter for deserialization
datalist = (ArrayList)bf.Deserialize(fs);
fileLoaded = true; //Update the fileLoaded bool so that it doesn't open the file dialog instance
recordCount = datalist.Count;
MessageBox.Show("" + filename + " loaded successfully."); //Inform the user that the file was automatically loaded
MessageBox.Show("Test: " + ((custDetails)datalist[0]).firstName);
}
catch
{
MessageBox.Show("Could not de-serialise from " + filename, "FILE LOADING PROBLEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
fs.Close();
}
catch
{
if (MessageBox.Show("File isn't in the right location, this is normal if a dataset does not yet exist.\n\n If the file exists elsewhere click no and you will be prompted to find the database file, else click yes to create a default file.", "FILE LOADING PROBLEM", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
fileLoaded = true;
CreateDefaultData();
}
}
我尝试了“字符串fn=custDetailsdatalist[0]。firstName;”以确保它不是导致问题的变量,并且异常仍然发生。我几乎没有主意了。问题不可能出在struct或arraylist定义上,因为LoadData中的messagebox工作正常并输出正确的信息。我尝试将messagebox移动到ViewData方法,这也开始出现异常,因此我只能假设我的方法有问题
这些方法在MainClass.cs上,下面是我如何从frmViewRecords调用该方法的:
MainClass mainClass = new MainClass();
int currentRecord = 0;
private void LoadData()
{
mainClass.ViewData(currentRecord);
}
值得一提的是,之前,我直接从frmViewRecords调用数据,如下所示:
txtFirstName.Text = ((MainClass.custDetails)mainClass.datalist[currentRecord].firstName;
但是在messagebox提示符工作时得到相同的异常之后,我将其重写到上面的代码,仍然得到了问题,因此我不知道是什么导致了它 数据列表中没有项目。LoadData中的recordCount的值可能也是零。试试这个:
if(datalist.Count != 0) { /* Get the current record */ }
什么是数据列表C类型?您是否在例外的行上中断了调试器并检查了datalist的内容是否符合预期?datalist是我的arraylist,它包含一个结构,其中firstName是一个字符串。虽然这确实停止了异常,但MessageBox如何。Show+custDetailsdatalist[0]。firstName;输出正确,但当我试图从另一个方法调用数据时,datalist显然是空的?datalist是这两种方法的全局arraylist,因此应该相同吗?我对C和structures/ArrayList非常陌生,所以我现在的理解最多是零碎的。可能是因为你从MainClass创建了一个新实例,但你没有从流中加载数据,所以datalist字段是空的。谢谢你指出这一点!我不知道何时加载数据,它不是所有表单的全局访问。简单地从ViewRecords调用LoadData似乎是可行的。我以为arraylist是托管在MainClass.cs上的,而不是从任何称为LoadData方法的形式。检查此链接:下面是一个关于C类成员的链接: