"Use of unassigned local variable" error

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

This is a little tricky, but I think the error I'm getting isn't
valid. The compiler just doesn't know my logic.

MyObject o;

switch (IntVariable)
{
case 1:
o = new MyObject()
break;
case 2:
o.MySetting = "Test Setting"
break;
}

The compiler tells me that I can use o.MySetting because "o" is
unassigned. Apparently it doesn't understand that case 1 will always
come before case 2. Is there a way to set things straight?

Dom
 
By the way, the code example I used was made too simple. The obvious
answer -- move "o = new MyObject()" outside the switch won't work.
The switch itself appears in a loop, and I need a new "MyObject" when
case is 1 each time through the loop.

Dom
 
Dom said:
This is a little tricky, but I think the error I'm getting isn't
valid. The compiler just doesn't know my logic.

That's why it's a valid error. The compiler is perfectly entitled to
not know your logic.
MyObject o;

switch (IntVariable)
{
case 1:
o = new MyObject()
break;
case 2:
o.MySetting = "Test Setting"
break;
}

The compiler tells me that I can use o.MySetting because "o" is
unassigned. Apparently it doesn't understand that case 1 will always
come before case 2. Is there a way to set things straight?

Well, hang on a sec - only one of those cases is going to be executed
anyway. Is there more relevant code you haven't shown us - like a loop
of some description?

Either way, the compiler is perfectly right to complain about this -
the variable isn't definitely assigned. Change the declaration to
assign the variable an initial value of null.
 
Dom,

Just set o to null before it is used:

MyObject o = null;

switch (IntVariable)
....

Hope this helps.
 
MyObject o = null ;

switch (IntVariable)
{
case 1:
o = new MyObject();
break;
case 2:

if(null!=o)
{

o.MySetting = "Test Setting";
}
else
{
throw new ArgumentIsNullException(" Yeah, I thought o was populated,
but it wasn't. Recheck my logic");
}
break;

}



That's "a" solution. But there is something gut-feeling wrong about it.
Are you looping?
You might post more of the code problem, because that code above just
doesn't look good.
 
Sloan and Nicholas ... thanks. Works fine.

Dom

Dom,

Just set o to null before it is used:

MyObject o = null;

switch (IntVariable)
...

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


This is a little tricky, but I think the error I'm getting isn't
valid. The compiler just doesn't know my logic.
MyObject o;
switch (IntVariable)
{
case 1:
o = new MyObject()
break;
case 2:
o.MySetting = "Test Setting"
break;
}
The compiler tells me that I can use o.MySetting because "o" is
unassigned. Apparently it doesn't understand that case 1 will always
come before case 2. Is there a way to set things straight?
 
Right. I said in my follow-up post that I mistakenly left out the
important fact that the switch is inside a loop. The "null" thingy
answered my question.

Dom
 
Back
Top