Why does the compiler say "a constant value is required" for the first case...the second case works fine...
switch (definingGroup)
{
case Properties.Settings.Default.OU_HomeOffice:
//do something
break;
case "OU=Home Office":
//do something
break;
default:
break;
}
also tried...
switch (definingGroup)
{
case Properties.Settings.Default.OU_HomeOffice.ToString():
//do something
break;
case "OU=Home Office":
//do something
break;
default:
break;
}
...same error
Here's the Properties.Setting code
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("OU=Home Office")]
public string OU_HomeOffice {
get {
return ((string)(this["OU_HomeOffice"]));
}
}
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier.
Explanation: Switch statement only supports Integral data types, Any other data types will throw an Invalid type error. Other examples for Invalid switch: switch(4.5) , switch(10.0/7.1), switch(a + 4.5) etc. The part of the case clause must be a constant integral value or a constant expression followed by a colon.
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
The syntax of the switch statement is: statement(s); break; case constant 2: statement(s);
Properties.Settings.Default.OU_HomeOffice
isn't a constant string - something known at compile time. The C# switch statement requires that every case is a compile-time constant.
(Apart from anything else, that's the only way it can know that there won't be any duplicates.)
See section 8.7.2 of the C# 3.0 spec for more details.
This is because the value cannot be determined at compile time (as it is coming out of a configuration setting). You need to supply values that are known at the time the code is compiled (constants).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With