Question

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.

Was it helpful?

Solution

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();
    }
}

OTHER TIPS

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

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top