none
"Use of unassigned local variable" Problem

    Question

  • Hi, I have a question that has probably been asked before, but I couldn't seem to find any answers that addressed my specific senario.

     

    I am writing a method that takes an array as its input paramater (array is contents of a file as generated by ReadAllLines). It searches the array for a string matching a regular expression, and returns a part of the regex.

    The code is this:

    // returns the name of the site from the file
    public string siteName(string[] allInFilesName)
    {
       string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
       string siteNameValue;
       foreach (string snArr in allInFilesName)
       {
         if (Regex.IsMatch(snArr, siteNamePattern))
         {
           Match snMatch = Regex.Match(snArr, siteNamePattern);
           siteNameValue = snMatch.Groups[2].Value;
         }
       }
       return siteNameValue;
    }
    

    The problem is that I get an error saying "Use of unassigned local variable 'siteNameValue'", with the "return siteNameValue;" underlined in red. For some reason, it appears that the "siteNameValue = snMatch.Groups[2].Value;" in the foreach loop is not actually assigning the value to the variable outside the loop.

    Does anyone have any ideas?

     

    It's even more fustrating in I have a very similar method that does work, only the other method returns a string array rather than a string...

     

    Thank you.



    Tuesday, July 19, 2011 9:22 AM

Answers

  • string siteNameValue = "";

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:30 AM
  • On 2011-07-19 11:22, sherm_p wrote:
     
    > The code is this:
    >
    > // returns the name of the site from the file
    > public  string  siteName(string[] allInFilesName)
    > {
    >     string  siteNamePattern =@"(\\b Site:\\tab\s+\[)(\w+)(\])";
    >     string  siteNameValue;
    >     foreach  (string  snArrin  allInFilesName)
    >     {
    >       if  (Regex.IsMatch(snArr, siteNamePattern))
    >       {
    >         Match snMatch = Regex.Match(snArr, siteNamePattern);
    >         siteNameValue = snMatch.Groups[2].Value;
    >       }
    >     }
    >     return  siteNameValue;
    > }
    >
    > The problem is that I get an error saying "Use of unassigned local
    > variable 'siteNameValue'", with the "return siteNameValue;" underlined
    > in red.
    Since the siteNameValue = snMatch.Groups[2].Value; is within an if
    statement and you have not set a default value. The compiler will
    conclude that you are using an unassigned variable.
     
    Give the siteNameValue a default value:
     string  siteNameValue = "";
    and the error should go away...
     
    // Anders
     
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:07 AM
    Tuesday, July 19, 2011 9:30 AM
  • Hello sherm_p,

    Hi, I have a question that has probably been asked before, but I couldn't seem to find any answers that addressed my specific senario.

     

    I am writing a method that takes an array as its input paramater (array is contents of a file as generated by ReadAllLines). It searches the array for a string matching a regular expression, and returns a part of the regex.

    The code is this:

     

    // returns the name of the site from the file
    public string siteName(string[] allInFilesName)
    {
     string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
     string siteNameValue;
     foreach (string snArr in allInFilesName)
     {
     if (Regex.IsMatch(snArr, siteNamePattern))
     {
     Match snMatch = Regex.Match(snArr, siteNamePattern);
     siteNameValue = snMatch.Groups[2].Value;
     }
    }
    
     return siteNameValue;
    }
    

     

    The problem is that I get an error saying "Use of unassigned local variable 'siteNameValue'", with the "return siteNameValue;" underlined in red. For some reason, it appears that the "siteNameValue = snMatch.Groups[2].Value;" in the foreach loop is not actually assigning the value to the variable outside the loop.

    Does anyone have any ideas?

     

    It's even more fustrating in I have a very similar method that does work, only the other method returns a string array rather than a string...

     

    Thank you.



    try to set your variable whith String.Empty; see this sample.

     public string siteName(string[] allInFilesName)
     {
      string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
     string siteNameValue = string.Empty;
    
     foreach (string snArr in allInFilesName)
      {
      if (Regex.IsMatch(snArr, siteNamePattern))
      {
       Match snMatch = Regex.Match(snArr, siteNamePattern);
       siteNameValue = snMatch.Groups[2].Value;
      }
      else
      {
       siteNameValue = string.Empty;
      }
      }
      return siteNameValue;
     }
    
    Bye
     http://community.visual-basic.it/carmelolamonica/

    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:32 AM
  • You do not initialize your return variable siteNameVariable. A assignment only happens in the loop/if branch which may not be executed. Thus the error message. The simple solution: Assign a default return value when declaring this variable:

    public string siteName(string[] allInFilesName)
    {
       string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
       string siteNameValue = null;
       foreach (string snArr in allInFilesName)
       {
         if (Regex.IsMatch(snArr, siteNamePattern))
         {
           Match snMatch = Regex.Match(snArr, siteNamePattern);
           siteNameValue = snMatch.Groups[2].Value;
         }
       }
        return siteNameValue;
    }

    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:33 AM

All replies

  • string siteNameValue = "";

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:30 AM
  • On 2011-07-19 11:22, sherm_p wrote:
     
    > The code is this:
    >
    > // returns the name of the site from the file
    > public  string  siteName(string[] allInFilesName)
    > {
    >     string  siteNamePattern =@"(\\b Site:\\tab\s+\[)(\w+)(\])";
    >     string  siteNameValue;
    >     foreach  (string  snArrin  allInFilesName)
    >     {
    >       if  (Regex.IsMatch(snArr, siteNamePattern))
    >       {
    >         Match snMatch = Regex.Match(snArr, siteNamePattern);
    >         siteNameValue = snMatch.Groups[2].Value;
    >       }
    >     }
    >     return  siteNameValue;
    > }
    >
    > The problem is that I get an error saying "Use of unassigned local
    > variable 'siteNameValue'", with the "return siteNameValue;" underlined
    > in red.
    Since the siteNameValue = snMatch.Groups[2].Value; is within an if
    statement and you have not set a default value. The compiler will
    conclude that you are using an unassigned variable.
     
    Give the siteNameValue a default value:
     string  siteNameValue = "";
    and the error should go away...
     
    // Anders
     
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:07 AM
    Tuesday, July 19, 2011 9:30 AM
  • Hello sherm_p,

    Hi, I have a question that has probably been asked before, but I couldn't seem to find any answers that addressed my specific senario.

     

    I am writing a method that takes an array as its input paramater (array is contents of a file as generated by ReadAllLines). It searches the array for a string matching a regular expression, and returns a part of the regex.

    The code is this:

     

    // returns the name of the site from the file
    public string siteName(string[] allInFilesName)
    {
     string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
     string siteNameValue;
     foreach (string snArr in allInFilesName)
     {
     if (Regex.IsMatch(snArr, siteNamePattern))
     {
     Match snMatch = Regex.Match(snArr, siteNamePattern);
     siteNameValue = snMatch.Groups[2].Value;
     }
    }
    
     return siteNameValue;
    }
    

     

    The problem is that I get an error saying "Use of unassigned local variable 'siteNameValue'", with the "return siteNameValue;" underlined in red. For some reason, it appears that the "siteNameValue = snMatch.Groups[2].Value;" in the foreach loop is not actually assigning the value to the variable outside the loop.

    Does anyone have any ideas?

     

    It's even more fustrating in I have a very similar method that does work, only the other method returns a string array rather than a string...

     

    Thank you.



    try to set your variable whith String.Empty; see this sample.

     public string siteName(string[] allInFilesName)
     {
      string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
     string siteNameValue = string.Empty;
    
     foreach (string snArr in allInFilesName)
      {
      if (Regex.IsMatch(snArr, siteNamePattern))
      {
       Match snMatch = Regex.Match(snArr, siteNamePattern);
       siteNameValue = snMatch.Groups[2].Value;
      }
      else
      {
       siteNameValue = string.Empty;
      }
      }
      return siteNameValue;
     }
    
    Bye
     http://community.visual-basic.it/carmelolamonica/

    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:32 AM
  • You do not initialize your return variable siteNameVariable. A assignment only happens in the loop/if branch which may not be executed. Thus the error message. The simple solution: Assign a default return value when declaring this variable:

    public string siteName(string[] allInFilesName)
    {
       string siteNamePattern = @"(\\b Site:\\tab\s+\[)(\w+)(\])";
       string siteNameValue = null;
       foreach (string snArr in allInFilesName)
       {
         if (Regex.IsMatch(snArr, siteNamePattern))
         {
           Match snMatch = Regex.Match(snArr, siteNamePattern);
           siteNameValue = snMatch.Groups[2].Value;
         }
       }
        return siteNameValue;
    }

    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    • Marked as answer by sherm_p Tuesday, July 19, 2011 10:08 AM
    Tuesday, July 19, 2011 9:33 AM
  • Thank you everyone for your replies and help.

     

    I had tried what had been suggested, but I still wasn't getting the results I should have been getting, so I thought the siteNameValue in the loop wasn't being updated with the values in the if statement.

    Turns out my regular expression needed tweaking....

     

    A tweaked regular expression, along with setting a default value for the variable is working perfectly.

     

     

    I feel a bit silly now

    Tuesday, July 19, 2011 10:13 AM
  • Hello sherm_p

    Thank you everyone for your replies and help.

     

    I had tried what had been suggested, but I still wasn't getting the results I should have been getting, so I thought the siteNameValue in the loop wasn't being updated with the values in the if statement.

    Turns out my regular expression needed tweaking....

     

    A tweaked regular expression, along with setting a default value for the variable is working perfectly.

     

     

    I feel a bit silly now


    you're welcome.

    Bye


     http://community.visual-basic.it/carmelolamonica/
    Tuesday, July 19, 2011 10:15 AM