当我编译这段代码时,我收到一个编译错误,说writer
is an unassigned local variable in SaveArray
. reader
它不会在类似的方法中抱怨LoadArray
。为什么会这样?他们不应该表现得一样吗?
static void SaveArray(string fileName, string[,] arr)
{
StreamWriter writer;
try
{
writer = new StreamWriter(fileName);
}
catch
{
MessageBox.Show("Error, could not open " + fileName + " for saving");
}
try
{
foreach (string entry in playerArray)
{
writer.WriteLine(entry);
}
}
catch
{
MessageBox.Show("Couldn't save");
}
writer.Close();
}
static void LoadArray(string fileName, string[,] arr)
{
StreamReader reader;
try
{
reader = new StreamReader( fileName );
}
catch
{
MessageBox.Show("Error when reading file" +fileName);
return;
}
try
{
for(int i=0; i<=arr.GetUpperBound(0); ++i)
{
for (int j = 0; j<=arr.GetUpperBound(1); ++j)
{
arr[i, j] = reader.ReadLine();
}
}
}
catch
{
MessageBox.Show("Could not read from file " +fileName);
}
reader.Close();
}