correct syntax for switch statement

30 views
Skip to first unread message

adiel

unread,
Dec 12, 2008, 11:04:40 AM12/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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

Cerebrus

unread,
Dec 12, 2008, 4:02:59 PM12/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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.

Cerebrus

unread,
Dec 12, 2008, 4:03:58 PM12/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On a side note, if you were to use the RadioButtonList control (quirks
notwithstanding), you would not have to do this kind of checking.

Samridhi Bhutani

unread,
Dec 14, 2008, 2:46:40 AM12/14/08
to DotNetDe...@googlegroups.com
try this

<asp:RadioButtonList ID="rd1" runat="server" OnSelectedIndexChanged="rd1_selectedIndexchanged" AutoPostBack="true">
    <asp:ListItem Text="a" Value="a"></asp:ListItem>
    <asp:ListItem Text="b" Value="b"></asp:ListItem>
    <asp:ListItem Text="c" Value="c"></asp:ListItem>
    </asp:RadioButtonList>
   

code :
protected void Set()
    {
        string livingArea = "";
        // Setup Living Area
        switch (rd1.SelectedItem.Text)
        {
            case "a":

                livingArea = "Rental Properties Listing";
                break;
            case "b":

                livingArea = "Home Property Listing";
                break;
            case "c":

                livingArea = "Apartment Property Listing";
                break;
            default:
                livingArea = "";
                break;
        }
        Response.Write(livingArea);

Cerebrus

unread,
Dec 14, 2008, 12:36:35 PM12/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
It appears to me that the OP is trying to evaluate the Checked
property of different RadioButton controls.
> > notwithstanding), you would not have to do this kind of checking.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages