All:
private void AppendData(string strData, string strFileName)
{
FileStream fStream;
StreamWriter sWriter;
try
{
fStream = new FileStream(strFileName, FileMode.Append);
sWriter = new StreamWriter(fStream);
sWriter.WriteLine(strData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sWriter.Close();
sWriter.Dispose();
fStream.Close();
fStream.Dispose();
}
}
The above code tells during compiling that "Use of unassigned local variable. It points to sWriter and fStream inside the finally block". Why I did it this way was, I read in a forum that .NET does not require us to explicitly initialize a variable. Is my understanding wrong ?
The code works fine if I initialize it to NULL.
private void AppendData1(string strData, string strFileName)
{
FileStream fStream = null;
StreamWriter sWriter = null;
try
{
fStream = new FileStream(strFileName, FileMode.Append);
sWriter = new StreamWriter(fStream);
sWriter.WriteLine(strData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sWriter.Close();
sWriter.Dispose();
fStream.Close();
fStream.Dispose();
}
}
Can you throw some light here...