Reputation: 21
How to get value in conditional statement in c# (example in a if statement and you want the value in the if statement be used outside it ) how?
example
int a, b, c;
if (a > 3)
{
c = 20;
}
else if (b < 3)
{
c = 10;
}
//how do i get the value of c outside the conditional statement??
Console.WriteLine("{0}", c);
//it always says local variable unassigned local variable
*update supposedly my code is correct and i ve declared a; and i want to use the value of a outside the conditional statement. *update all i want to know is to get the value of c outside the conditional
Upvotes: 0
Views: 2085
Reputation: 101701
It seems you declared a
but didn't initialize it.In C#
you should initialize local variables to something before the first usage. If your if
statement evaluates to false
then a
will remain uninitialized.And even if it would evalute to true
you are trying to read its value (in x++
) before initializing it, so it will still be a problem. To fix that just initialize it with a default value while declaring:
int a = 0;
Upvotes: 3
Reputation: 2164
Your problem is not c
at all; your problems are a
and b
. You just need a bit more understanding how the compiler works.
Let's put it this way: you have...
int a, b, c;
Here, you're telling the compiler that it will use 3 variables that somewhere, eventually, will contain int
values; but now they don't have nothing, they're empty (note: not 0; empty) or as known in C#, they're null
.
When the compiler reaches here:
if (a > 3)
{
c = 20;
}
it says: "whoops! I need to check if a
is less than 3, but before this line a
is never assigned a value, it's empty, and I can't check it like that. I'll better throw an error."
Same happens with the next condition.
If, as other answers say, before the condition you assign them a value, the compiler will be able to compare and use them.
Now let's take a look at your original code:
int a;
//I'll asume you declared a the same way that the other code
if (condition)//this is true
{
a = a++;
}
//how do i get the value of a?
Console.WriteLine("{0}", a);
//it always says local variable unassigned local variable
When the compiler reaches to Console.WriteLine("{0}", a);
, it says: "Hmmm... I have to print a
, but it has a value only if condition
is true
, but if it is false, I won't be able to print it because it'll be empty. Better throw an error now than when running!"
As you see, is all about using your variables only after you're sure that all possible ways that lead to your line of code assign a value to that variable.
Upvotes: 0
Reputation: 2215
In your new code example, the problem is that you've declared variables a
, b
, and c
but have not initialized them. An if statement might be entered and it might not be entered so any assignments done inside of an if statement might not be executed.
You need to tell the compiler what the initial values of a, b and c are before you can use them, which I think almost everyone here has already been saying. Try changing your code to this:
int a = 0, b = 0, c = 0;
if (a > 3)
{
c = 20;
}
else if (b < 3)
{
c = 10;
}
//This should print out 10
Console.WriteLine("{0}", c);
//no more compile errors will occur
Notice that a
, b
, and c
have been given default values of 0
so in the event that the if statement is not entered, they will still be assigned a usable value.
Also, if a = 0
and b = 4
then the entire if block is skipped, leaving c
untouched which is where its default value of 0
will be printed to the console.
Another option, which isn't the best thing to do in my opinion is the following
int a = 0, b = 0, c;
if (a > 3)
{
c = 20;
}
else if (b < 3)
{
c = 10;
}
else
{
c = 1;
}
Console.WriteLine("{0}", c);
This will compile even though you're not initializing the c
variable because in a round about way you are; in the final else
the variable is assigned 1 so there would be no case were c
is not initialized.
Upvotes: 0
Reputation: 20129
In this case a
should be declared outside the if
statement. IE
int a = 1;
if(condition){
a=a++;
}
console.WriteLine("{0}",a);
However, there are several issues with your code. Most notably, are you sure you want to be doing a=a++
? This code is redundant, you should be doing just a++
.
I suspect your real issue lies outside the code you shared. If you post more code I can refine my answer to help you more.
Upvotes: 1