Doubt in Switch-Case statement.
-
Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.
-
Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.
-
He is Cool wrote:
switch { case classname.myval: // gives compiler error }
in switch steatement you should set any value .
yup. i am giving a static value.
-
Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.
He is Cool wrote:
Would be much happy to know the logic behind this
Only constants are known at compile time. The value must be known at compile time for it to generate a valid switch statement. With a static the value can be changed at any time during the run of the program. With a readonly the value is set at runtime, although it cannot be changed once set. So, you can only provide a constant value. Either a literal value or something set as a
const
, just as the compiler says.
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Hi, I have a basic doubt in using a switch case. For the case stmt am unable to have a static or readonly value. The compiler gives a error "A constant value is expected". i.e, if i have a variable like - private static myval = "R1"; -- switch { case classname.myval: // gives compiler error } Would be much happy to know the logic behind this. Thanks.
switch is pretty much limited to constant values. The value needs to be known at compile time. myval can be changed in the code, so it will not always be the same.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
He is Cool wrote:
switch { case classname.myval: // gives compiler error }
in switch steatement you should set any value .
:confused:
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
:confused:
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
Yeah, that was definately a thinker...
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
switch is pretty much limited to constant values. The value needs to be known at compile time. myval can be changed in the code, so it will not always be the same.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Damn... Beat me to it!
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog