For the C# switch statement to work, all the possible values of your
cases MUST be known at compile time. Using a variable precludes this
necessity therefore your code doesn't compile. Also, you are trying to
circumvent the kind of scenario for which a switch case statement is
perfect by twisting it (the switch(true) statement, for instance).

In your scenario, an "if - else if - else" construct might be more
suitable.

On Dec 12, 9:04 pm, adiel <[email protected]> wrote:
> Hello, this must be an easy one.  I want to perform code based on the
> selected item from a radio button in asp.net.  I have the following
> code:
>
>                 // Setup Living Area
>                 switch (true)
>                 {
>                     case radRental.Checked:
>                         livingArea = "Rental Properties Listing";
>                         break;
>                     case radHome.Checked:
>                         livingArea = "Home Property Listing";
>                         break;
>                     case radApartment.Checked:
>                         livingArea = "Apartment Property Listing";
>                         break;
>                     default:
>                         livingArea = "";
>                         break;
>                 }
>
> I am getting an error stating "a constant value is expected" on each
> item in the case statement.  I know I must be missing a cast or
> something, can someone help?
>
> Thanks,
> Adiel

Reply via email to