PDA

Click to See Complete Forum and Search --> : How to get around this.


wossname
Dec 2nd, 2003, 03:48 AM
I get compiler error: Use of unassigned local variable 'MySW'

The line in the Finally block causes this. It is clearly being assigned to in the Try {} block. What gives?

//C#
void SeriousErrorMessage(string msg, string category)
{
StreamWriter MySW;
try
{
//write the error message to a local file on the HDD
MySW = new StreamWriter(itsLocalLogPath + "_" + category + "_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToShortTimeString() + EXTN_LOG_FILE, false);
MySW.Write(msg);
}
finally
{
MySW.Close(); //close the new file
}
}

Pirate
Dec 2nd, 2003, 04:48 AM
Declare MySW outside the void with private modifier .

dynamic_sysop
Dec 2nd, 2003, 06:12 AM
you can also declare the StreamWriter with a null , like this ...

void SeriousErrorMessage(string msg, string category)
{
StreamWriter MySW=null; // initialize it with a null .
try
{
//write the error message to a local file on the HDD
MySW = new StreamWriter(@"C:\some path.txt",false);
MySW.Write(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MySW.Close(); //close the new file
}
}

wossname
Dec 2nd, 2003, 08:11 AM
Thanks, I'll use Sysop's.