I've got problem with writing a converter from hex to bin, dec to bin etc. Here's my code, when I debug it I've got an error "Use of unassigned local variable Dec_Int10", could you help me? How can I fix this error?
protected void Button_Click (object sender, Event Args e)
{
if (Page.IsValid)
{
int Dec_Int10;
if(!(string.IsNullOrEmpty(TextBox1.Text)))
{
Dec_Int10 = Convert.ToInt32(TextBox1.Text, 10)));
}
if(!(string.IsNullOrEmpty(TextBox2.Text)))
{
Dec_Int10 = Convert.ToInt32(TextBox2.Text, 16)));
}
if(!(string.IsNullOrEmpty(TextBox3.Text)))
{
Dec_Int10 = Convert.ToInt32(TextBox3.Text, 8)));
}
if(!(string.IsNullOrEmpty(TextBox4.Text)))
{
Dec_Int10 = Convert.ToInt32(TextBox4.Text, 2)));
}
string Dec_Str10 = Convert.ToString(Dec_Int10, 10);
string Hex_Str16 = Convert.ToString(Dec_Int10, 16);
string Oct_Str8 = Convert.ToString(Dec_Int10, 8);
string Bin_Str2 = Convert.ToString(Dec_Int10, 2);
TextBox1.Text = Dec_Str10;
TextBox2.Text = Hex_Str16;
TextBox3.Text = Oct_Str8;
TextBox4.Text = Bin_Str2;
}
}
You get that error because there is no default value assigned to the variable and since the only assignments to it are inside if blocks, the compiler thinks there is a chance the variable will never be assigned.
If you just initialize it to 0 you'll no longer have the error.
This is one of the checks provided by the compiler to keep you from making easy-to-make mistakes.
You want to initialize your variables:
int Dec_Int10 = 0;
Otherwise the compiler doesn't know if it has ever been assigned a value.
You have never initialized Dec_Int10
, it's complaining because you have all those if
statements, the compiler can't guarantee it's going to satisfy one of the if
statements and set Dec_Int101
.
Either add an else
statement or try initializing it to:
int Dec_Int10 = -1;
Declare Dec_Int10 to be equal to zero (or whatever default value you want it to have) when you declare it.
You simply have to change
int Dec_Int10
to
int Dec_Int10 = 0;
since the compiler can't verify that Dec_Int10
will be set before it's used in another way.
adding an else would be the best thing to do. If you initialize it to begin with, you most likely will just reset the value. using the else will be more efficient.
Take a look at your code.
What happens if txtBox1,2,3 & 4 are ALL empty/null at the same time?
Nothing ever gets assigned to Dec_Int10. Thus later in the code you are attempt to convert an uninitialized variable. The compiler looks at the execution path and has determine that a scenario exists (such as all 4 if fail) that the variable isn't initialized.
As others have indicated you can resolve it by using:
int Dec_Int10 = 0;
The larger learning point is WHY it is happening?
This code for example won't generate an error because no matter what the status of textbox 1,2,3,4 Dec_Int10 will be initilialized before conversion.
int Dec_Int10;
if (!(string.IsNullOrEmpty(TextBox4.Text)))
Dec_Int10 = Convert.ToInt32(TextBox4.Text, 2);
else if (!(string.IsNullOrEmpty(TextBox3.Text)))
Dec_Int10 = Convert.ToInt32(TextBox3.Text, 8);
else if (!(string.IsNullOrEmpty(TextBox2.Text)))
Dec_Int10 = Convert.ToInt32(TextBox2.Text, 16);
else if (!(string.IsNullOrEmpty(TextBox1.Text)))
Dec_Int10 = Convert.ToInt32(TextBox1.Text, 10);
else
Dec_Int10 = 0;
TextBox1.Text = Convert.ToString(Dec_Int10, 10);
TextBox2.Text = Convert.ToString(Dec_Int10, 16);
TextBox3.Text = Convert.ToString(Dec_Int10, 8);
TextBox4.Text = Convert.ToString(Dec_Int10, 2);