473,408 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

"Use of unassigned local variable" error

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
8 11127
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


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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
4
by: octavio | last post by:
Hello members of the comp.lang.c newsgroup. Please I need you help on the following one. Compiling the simple code I'm getting this error message. Why ? Please what's the correct type of the fb...
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
3
by: ashokakr | last post by:
Hi all This my procedure, when i try to run the procedure it says "coitem_linenumber" is not a scalar variable. Can any one say what is the error and how i can correct it declare wotype...
3
Markus
by: Markus | last post by:
I was just playing around with javascript today and i wanted to change the style of a table element with javascript. The code i am using: ChangeIt(id){ var target = document.getElementById(id)...
2
by: plasmay | last post by:
Hi, I am new in learning c#, and have recently encountered a problem: static float ComputeAvg(float a) { float sum; int i; for (i = 0; i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.