Profundal insight #354: switch statement needs dummy assignment tocompile

R

RayLopez99

Notice the dummy assignment is needed below.

No I'm NOT a dummy.

Just pointing out a quirk in C#

RL

foreach (char x in myCharArray)

{

Char1 = 'Z'; //dummy assignment

if (Char.IsDigit(x)) Char1 = 'A';
if (Char.IsLetter(x)) Char1 = 'B';

switch (Char1)
{
case 'A':
{
break;
}
case 'B':
{
break;
}
default:
break;
}

}
 
G

Göran Andersson

RayLopez99 said:
Notice the dummy assignment is needed below.

No I'm NOT a dummy.

Just pointing out a quirk in C#

RL

foreach (char x in myCharArray)

{

Char1 = 'Z'; //dummy assignment

if (Char.IsDigit(x)) Char1 = 'A';
if (Char.IsLetter(x)) Char1 = 'B';

switch (Char1)
{
case 'A':
{
break;
}
case 'B':
{
break;
}
default:
break;
}

}

Of course you need to assign a value to the variable. The compiler
doesn't let you use a variable which possibly is unassigned.'

In this case it saves you from a bug in your code. If the variable would
be initialized when you enter the method (like it is in VB), so that you
didn't need to initialise it, it would keep it's value from one
iteration to the next in the loop. If you have a letter followed by
characters that are neither a digit or a letter, those character would
also be considered as letters.
 
R

RayLopez99

Oh, and of course it has nothing at all to do with the switch statement.

I doubt it Goran, but thanks for your answer. You can have an
unassigned variable int jay; for example, last I checked (let me check
now... yes, you just get a warning: "variable 'jay' is declared but
never used" but it compiles).

RL
 
B

Ben Voigt [C++ MVP]

RayLopez99 said:
I doubt it Goran, but thanks for your answer. You can have an
unassigned variable int jay; for example, last I checked (let me check
now... yes, you just get a warning: "variable 'jay' is declared but
never used" but it compiles).

It compiles only if you never read from it.

If you read from a variable that is potentially unassigned, you get a
compile error. Potentially unassigned simply means the compiler couldn't
guarantee it was written to already, its rules for this aren't perfect. But
it doesn't matter whether you use switch, if, add the variable to something
else, or so on. Anything that reads the variable will cause the error.
 
R

RayLopez99

If you read from a variable that is potentially unassigned, you get a
compile error.  Potentially unassigned simply means the compiler couldn't
guarantee it was written to already, its rules for this aren't perfect.

I think the key is it depends on what level your compiler warnings are
set. Mine are at "default" (whatever that is) and like I said in the
OP for case/switch it fails to compile, but for a regular int
declaration it just gives a warning.

Thanks for your input.

RL
 
H

Hans Kesting

RayLopez99 expressed precisely :
I think the key is it depends on what level your compiler warnings are
set. Mine are at "default" (whatever that is) and like I said in the
OP for case/switch it fails to compile, but for a regular int
declaration it just gives a warning.

Thanks for your input.

RL

Let's try to explain with some examples:


Try changing your code to

foreach (char x in myCharArray)
{
char Char1;
// Char1 = 'Z'; forget the dummy assignment

if (Char.IsDigit(x)) Char1 = 'A';
else if (Char.IsLetter(x)) Char1 = 'B'; // "else if"
else Char1 = 'Z'; // extra "else"

switch (Char1)
{
case 'A':
{
break;
}
case 'B':
{
break;
}
default:
break;
}
}

This will compile (and execute correctly), as Char1 is definitely
assigned by the time you hit the switch.


Or try this:

string s = "";
foreach (char x in myCharArray)
{
char Char1;
if (Char.IsDigit(x)) Char1 = 'A';
if (Char.IsLetter(x)) Char1 = 'B';

s += Char1;

}

This will *not* compile with the same error: you try to *read* a
variable (Char1) that possibly has not been assigned a value (*you* may
know that this routine will only get Digits or Letters, but the
compiler doesn't)


Hans Kesting
 
G

Göran Andersson

RayLopez99 said:
I doubt it Goran, but thanks for your answer. You can have an
unassigned variable int jay; for example, last I checked (let me check
now... yes, you just get a warning: "variable 'jay' is declared but
never used" but it compiles).

RL

I never said that you can't have an unassigned variable, I said that you
can't _use_ an unassigned variable.

Before doubting something, you should make sure that you at least read
it properly...
 
R

RayLopez99

This will *not* compile with the same error: you try to *read* a
variable (Char1) that possibly has not been assigned a value (*you* may
know that this routine will only get Digits or Letters, but the
compiler doesn't)

Hans Kesting

Thanks Hans. Essentially you are doing a workaround as in my "dummy
variable" assignment. I don't really see the difference. Thanks for
your input.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top