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 8 11127
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
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
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
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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |