2

Im trying to use a case statement to assign a value to a variable named "query". Depending on the value of a comboBox, the value of query will change. I assigned the "query" variable inside my method and want to use it within the method only. I get an error message that the "query" variable is unassigned even though it is being assigned at the top of the method. I have a work around but I dont know why this happens? any insight would be helpfull.

heres the code.

public void ExportKml()
    {
        string query;

            switch (txtTable.SelectedIndex)
            {
                case 0:
                    query = "Select * from dbo.HyacinthWaterBodyZones";
                    break;
                case 1:
                    query="Select * from lchcd.privateWatersFinal where waterbodypolygon is not null";
                    break;
                case 2:
                    query = "Select * from lchcd.publicWatersFinal where waterbodypolygon is not null";
                    break;

            }

            cs.Open();
            SqlCommand cmd = new SqlCommand(query, cs);   <<--Error Message 
            SqlDataReader polygon = cmd.ExecuteReader();
}

the "query" variable inside the line: SqlCommand cmd = new SqlComman(query,cs) gives an error stating it is unassigned local variable.

Nate S.
  • 1,117
  • 15
  • 31
  • 3
    While your selected index may never *logically* be anything other than 0, 1, or 2, the compiler has no way of knowing this. You need to either initialize the variable with a dummy value, or add a `case else` that sets it to a dummy value. – jbabey Sep 19 '13 at 15:46

3 Answers3

3

You need to asign an initial value to query. Either "" or string.Empty. You should include a default case in your switch also. See below:

public void ExportKml()
{
    string query = string.Empty;

    switch (txtTable.SelectedIndex)
    {
        case 0:
            query = "Select * from dbo.HyacinthWaterBodyZones";
            break;
        case 1:
            query = "Select * from lchcd.privateWatersFinal where waterbodypolygon is not null";
            break;
        case 2:
            query = "Select * from lchcd.publicWatersFinal where waterbodypolygon is not null";
            break;
        default:
            query = "";
            break;
    }

    // Add a check for empty string before trying the query.
    if(!string.IsNullOrWhiteSpace(query))
    {
        cs.Open();
        SqlCommand cmd = new SqlCommand(query, cs);
        SqlDataReader polygon = cmd.ExecuteReader();
    }
}
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • The `default` case isn't needed, why **should** he include it anyways? – Philipp M Sep 19 '13 at 15:56
  • default case isnt needed but it was helpfull to know. Easy fix but it was giving me a headache – Nate S. Sep 19 '13 at 16:00
  • 1
    @CodeSlinger, read http://stackoverflow.com/questions/4649423/should-switch-statements-always-contain-a-default-clause – Sam Leach Sep 19 '13 at 16:05
  • @sam Leach, In my case the default isnt needed because the method does not run unless the user chooses an item in the combobox. Hence the case statment always returns a value from 0 to 2. Helpfull link though thanks! – Nate S. Sep 19 '13 at 16:11
  • Make sure you write some tests that prove your assumption. :) – Sam Leach Sep 19 '13 at 16:16
3

Because the compiler has no way to know if your SelectedIndex will be always 0,1,or 2, so it gives you a strong suggestion to initialize the variable defined before the switch but used after it

string query = string.Empty;
switch(.....)
{
    ....
}
if(query.Length > 0)
{
    cs.Open();
    SqlCommand cmd = new SqlCommand(query, cs);   <<--Error Message 
    SqlDataReader polygon = cmd.ExecuteReader();    
}

Adding a default case to the switch statement works also, but, personally, I prefer to initialize the query variable before entering the switch statement. It is more clear to me and less prone to forgetting some other important initializations

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Maybe use `string.IsNullOrWhiteSpace(query)` ? – Sam Leach Sep 19 '13 at 15:52
  • @SamLeach I am not sure, in this case it seems pointless because I already know that the string is not null and calling a method seems more expensive while a property should be faster. However these are just micro-optimizations. – Steve Sep 19 '13 at 15:59
2

If the selected index is not 0-2, you will not hit any of the case statements. You need to include a default: case if you are going to assign a variable in the switch.

switch (txtTable.SelectedIndex)
{
    case 0:
        query = "Select * from dbo.HyacinthWaterBodyZones";
        break;
    case 1:
        query="Select * from lchcd.privateWatersFinal where waterbodypolygon is not null";
        break;
    case 2:
        query = "Select * from lchcd.publicWatersFinal where waterbodypolygon is not null";
        break;
    default:
        //assign query = something here
        break;
}
mao47
  • 967
  • 10
  • 25