Select Case Statement AND Switch Statement:

A web programmer can write code that performs a response based on the answer given to the available choices. Radio button controls are good examples of when this scenerio might be useful. For VB.NET programmers, a "Select Case Statement" is used and for C# web programmers a "Switch Statement" is used. Below is example code for both languages as well as an interactive example showing an example of a web page that uses a select case statement.

Give a response for every choice available.

A web programmer can use "select case" statements to create a dynamic web page that displays varying results based upon the choice the user selects.  See a select case statement in action.

 





candy

VB.NET code for the Select Case Statement (String):

  
 dim candy as string = radCandy.SelectedValue
    
     Select Case candy
       Case "gummybears"
            img1.ImageURL="images/candy/gummybears.jpg"
       Case "candycorn"
            img1.ImageURL="images/candy/candycorn.jpg"
       Case "kisses"
            img1.ImageURL="images/candy/kisses.jpg"
       Case Else
           img1.visible="false"
    End Select
    
 



Below is the exact same web page programmed in C# instead of VB.NET.

C# code for the Switch Statement (Strings):

  
{ 
    string candy = radCandy.SelectedValue; 
    
    switch (candy) { 
        case "gummybears": 
            img1.ImageURL = "images/candy/gummybears.jpg"; 
            break; 
        case "candycorn": 
            img1.ImageURL = "images/candy/candycorn.jpg"; 
            break; 
        case "kisses": 
            img1.ImageURL = "images/candy/kisses.jpg"; 
            break; 
        default: 
            img1.visible = "false"; 
            break; 
    } 
} 

 

NOTE: I have experienced that when you try to put more than 1 case on the same line it doesn't work right. So each scenerio needs a new "case".

VB.NET vs. C#

Variables

Decisions

Built In Functions