By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

"Use of unassigned local variable" error

P: n/a
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

Apr 30 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

On Apr 30, 2:48 pm, Dom <dolivas...@gmail.comwrote:
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

Apr 30 '07 #2

P: n/a


MyObject o = null;

if
case 2:
o.MySetting = "Test Setting"
break;

runs..... o will be null. thus why you're getting the error.


"Dom" <do********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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

Apr 30 '07 #3

P: n/a
Dom,

Just set o to null before it is used:

MyObject o = null;

switch (IntVariable)
....

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dom" <do********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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

Apr 30 '07 #4

P: n/a
Dom <do********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 30 '07 #5

P: n/a
Dom
Sloan and Nicholas ... thanks. Works fine.

Dom

On Apr 30, 2:59 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Dom,

Just set o to null before it is used:

MyObject o = null;

switch (IntVariable)
...

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Dom" <dolivas...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
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

Apr 30 '07 #6

P: n/a
No, it wasn't too simple. You really do have an unassigned variable.

What you want to do is this:

MyObject o=null;

-- now it's not "unassigned". It's null.

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Dom" wrote:
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

On Apr 30, 2:48 pm, Dom <dolivas...@gmail.comwrote:
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


Apr 30 '07 #7

P: n/a

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.



"Dom" <do********@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
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

On Apr 30, 2:48 pm, Dom <dolivas...@gmail.comwrote:
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


Apr 30 '07 #8

P: n/a
Dom
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

On Apr 30, 2:57 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Dom <dolivas...@gmail.comwrote:
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.