-2

I am new to programming and I want to pass the value of string variable "serverPath" to string variable "DestinationPath". however, I am having this error "use of unassigned variable". this is my code:

        string DestinationPath;
        string serverPath;

        if (ServerList.SelectedItem.Value == "1")
        {
            serverPath = "10..13.58.17";
        }
        else if (ServerList.SelectedItem.Value == "2")
        {
            serverPath = "10..13.58.33";
        }

        DestinationPath = @"\\"+serverPath+"\C$\TEST FOLDER\DESTINATION FOLDER";

what am I doing wrong here? how can I pass the value of serverPath outside the If-statement? Any help will be appreciated. Thank you.

NoobNoob
  • 3
  • 3
  • you should always have an `else`, and always initialize the variable – jazb Jan 24 '20 at 05:47
  • you have declared the variable outside the if block, so it will be in scope from outside the if block – jazb Jan 24 '20 at 05:48
  • 1
    "_you should always have an else_" - No you should not _always_ have an `else`. If there is an empty `else` you should refactor (easiest way: remove it) your code instead of cluttering it with unnescessary constructs. – Markus Deibel Jan 24 '20 at 06:03
  • Occasionally the C# compiler will emit an error message that seems obscure and difficult to comprehend. This is not one of those cases. The error message is telling you exactly what it means: you have a statement in the code that is using a variable that the compiler cannot prove has been assigned. In your example above, if the value checked is neither `"1"` nor `"2"`, the `serverPath` value will not have been assigned. See marked duplicates for much more detail on the variations on how this error can occur and techniques for dealing with it. – Peter Duniho Jan 24 '20 at 06:18

3 Answers3

0

With your if statements you are looking for one of two different conditions and setting the serverPath accordingly. But it is possible in your code that none of these conditions are met, and that the variable will not be set at all. This is why you are getting the error.

The solution is either one the following:

If you can be sure that you will only have "1" or "2" then change your second else if to just else

if (ServerList.SelectedItem.Value == "1")
{
    serverPath = "10..13.58.17";
}
else
{  
   //Must be "2"
   serverPath = "10..13.58.33";
}

or add another default case

if (ServerList.SelectedItem.Value == "1")
{
    serverPath = "10..13.58.17";
}
else if (ServerList.SelectedItem.Value == "2")
{
   serverPath = "10..13.58.33";
}
else
{
   serverPath = "10..99.99.99";
}

or you need to set a default (or empty) value when you declare the variable.

string serverPath = "";  //Please note, this can still lead to bugs in your code!

or

string serverPath = "10..99.99.99";
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
0

In C# a variable must be initialized before using it

first you do

string serverPath = string.Empty;

and after all your conditions

if(!string.IsNullOrEmpty(serverPath))
{
   DestinationPath = @"\\"+serverPath+"\C$\TEST FOLDER\DESTINATION FOLDER";
}
Hussain Md
  • 119
  • 1
  • 11
-1

assign your variable as

     string DestinationPath = string.Empty;
     string serverPath = string.Empty;

Hope, this works.

and instead of using if-else-if you can use switch statement

Vikrant Jain
  • 135
  • 1
  • 11