|
-
Dec 2nd, 2003, 04:48 AM
#1
How to get around this.
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?
Code:
//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
}
}
I don't live here any more.
-
Dec 2nd, 2003, 05:48 AM
#2
Sleep mode
Declare MySW outside the void with private modifier .
-
Dec 2nd, 2003, 07:12 AM
#3
you can also declare the StreamWriter with a null , like this ...
VB Code:
[Color=Blue]void[/color] SeriousErrorMessage([Color=Blue]string[/color] msg, [Color=Blue]string[/color] category)
{
StreamWriter MySW=[Color=Blue]null[/color]; [Color=Green]//[/color] [Color=Green]initialize[/color] [Color=Green]it[/color] [Color=Green]with[/color] [Color=Green]a[/color] [Color=Green]null[/color] [Color=Green].[/color] [Color=Green]
[/color] [Color=Blue]try
[/color] {
[Color=Green]//write[/color] [Color=Green]the[/color] [Color=Green]error[/color] [Color=Green]message[/color] [Color=Green]to[/color] [Color=Green]a[/color] [Color=Green]local[/color] [Color=Green]file[/color] [Color=Green]on[/color] [Color=Green]the[/color] [Color=Green]HDD
[/color] MySW = [Color=Blue]new[/color] StreamWriter(@"C:\some path.txt",[Color=Blue]false[/color]);
MySW.Write(msg);
}
[Color=Blue]catch[/color] (Exception ex)
{
MessageBox.Show(ex.Message);
}
[Color=Blue]finally
[/color] {
MySW.Close(); [Color=Green]//close[/color] [Color=Green]the[/color] [Color=Green]new[/color] [Color=Green]file
[/color] }
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 2nd, 2003, 09:11 AM
#4
Thanks, I'll use Sysop's.
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|