"Jim Langston" <ta*******@rocketmail.comwrote in message
news:qn************@newsfe04.lga...
"vsgdp" <he***@null.comwrote in message
news:u8nOg.9542$cw.2165@fed1read03...
>Why can't I define variables in a case statement:
case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want
11 to map to an enum element NOVEMBER.
case 1:
{
float u = expression;
// ...
break;
}
case 2:
// ....
Sorry, missed the 2nd part of your question, re casting ints to enums.
Casting a number as a month, would be okay *some of the time*. What
happens, however, when the value is 13?
enum Months
{
January = 1,
February,
March,
// ...
December
}
int Month = (something).
Going from Months to Month wouldn't be a problem as such, as you can examine
the value. if ( Month 13 || Month < 1 ) { some error }.
But when you go into a variable declared as Months
Months EnumMonth = Month; // maybe some cast required
What re you going to do if Month is 13? In a switch statement it shouldn't
be that difficult as long as you remember to use default:
I go from ints to enums all the time when serializing my classes (writing
them to files or databases) but I ensure that the int value came from an
enum, not from some other form of input (such as user input) without
checking.
I may have understood your question wrong, however. You may wish to
rephrase it as to what it is you would actually want to do.