1

I am creating a small program where the value of the radiobutton will be added into the database (gender: male and female.) But it has an error saying "use of unassigned local variable" on the "gender" that must be inserted into the database. Any help will be appreciated.

here is my code:

private void button1_Click(object sender, EventArgs e)
    {
        Connection();
        sql_connect.Open();

        try
        {


            string gender;
            if (radioButton_Female.Checked)
            {
                gender = "Female";
            }

            if (radioButton_Male.Checked)
            {   
                gender = "Male";

            }
            sql_command = new MySqlCommand("insert into tbl_sample (gender) values ('" + gender + "')", sql_connect); //here
            sql_command.ExecuteNonQuery();


        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


        sql_connect.Close();


        MessageBox.Show("Added!");

    }
Naomi
  • 131
  • 1
  • 3
  • 13
  • 1
    You have 2 separate if statements, so it is possible that `gender` could never be assigned a value if neither of these are true. Realistically this probably wouldn't happen, so just initialize to empty: `string gender = "";` – Jason Faulkner Dec 14 '15 at 15:17
  • 1
    You better use `SQL parameters` to prevent from `SQL injections` – Sybren Dec 14 '15 at 15:20
  • Set your gender string to an empty string and then you can also simplify your logic using the conditional operator as `gender = radioButton_Male.Checked ? "Male" : "Female";` – Izzy Dec 14 '15 at 15:28
  • gender will never have a value outside of the if statement. Have a empty string outside and assign the value if the condition is met. –  Dec 14 '15 at 15:31

3 Answers3

2

You have to provide a default-value for gender if none of your conditions will pass. Although that may not happen within your application, the compiler isn´t smart enough to know that. So write this:

string gender = string.Empty;

Alternativly replace the second if-statement by an else:

if (radioButton_Female.Checked)
{
    gender = "Female";
}
else
{   
    gender = "Male";
}

Or even shorter using the ternary operator:

string gender = radioButton_Female.Checked ? "Female" : "Male"

Thus gender is allways initialized.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

What if your both radio button not checked? What gender will be in this case?

Compiler can't know that. It can never be sure that gender is actually initialized. That's why you need to initialize your local variable before you use it. Compiler gives this error because it was trying to prevent making a mistake.

As Eric Lippert said;

Use of an unassigned local variable is a likely bug, and this can be detected by the compiler at low cost.

Assign it as;

string gender = "";

A few things more;

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

If you follow the execution of the code there is a chance that the string gender could not be assigned. if both radioButton_Male and radioButton_Female are not checked. This may not be possible within the logic of your UI, But from a compiler perspective it is.

You could initialise the gender string to something like string.Empty. Or preferably you could change your logic to use an if else structure. for example:

        string gender = string.Empty;
        if (radioButton_Female.Checked)
        {
            gender = "Female";
        }else if (radioButton_Male.Checked)
        {   
            gender = "Male";

        }else
        {
            throw new Exception("Error msg");
        }
Lewis Taylor
  • 673
  • 3
  • 13