Results 1 to 4 of 4

Thread: How to get around this.

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Declare MySW outside the void with private modifier .

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you can also declare the StreamWriter with a null , like this ...
    VB Code:
    1. [Color=Blue]void[/color] SeriousErrorMessage([Color=Blue]string[/color] msg, [Color=Blue]string[/color] category)
    2.         {
    3.              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]
    4. [/color]           [Color=Blue]try
    5. [/color]           {
    6.                 [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
    7. [/color]                 MySW = [Color=Blue]new[/color] StreamWriter(@"C:\some path.txt",[Color=Blue]false[/color]);
    8.                  MySW.Write(msg);
    9.             }
    10.             [Color=Blue]catch[/color] (Exception ex)
    11.             {
    12.                  MessageBox.Show(ex.Message);
    13.             }
    14.             [Color=Blue]finally
    15. [/color]           {
    16.                  MySW.Close(); [Color=Green]//close[/color] [Color=Green]the[/color] [Color=Green]new[/color] [Color=Green]file
    17. [/color]           }
    18.         }
    ~
    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]

  4. #4

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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
  •  



Click Here to Expand Forum to Full Width