Getting Selective with Select Case


The If ... Then ... ElseIf ... Else ... End If statement is a very simple and useful way to make decisions in your programs. Similarly, a Select Case statement runs different code blocks, based on the evaluation of an expression. Imagine that you have a program that does different things depending on the day of the week. You would have something like this:

If DateTime.Today.DateOfWeek.ToString() = "Sunday" Then   'Do something on Sunday ElseIf DateTime.Today.DateOfWeek.ToString() = "Monday" Then   'Do something on Monday ElseIf DateTime.Today.DateOfWeek.ToString() = "Tuesday" Then   'Do something on Tuesday ElseIf DateTime.Today.DateOfWeek.ToString() = "Wednesday" Then   'Do something on Wednesday ElseIf DateTime.Today.DateOfWeek.ToString() = "Thursday" Then   'Do something on Thursday ElseIf DateTime.Today.DateOfWeek.ToString() = "Friday" Then   'Do something on Friday ElseIf DateTime.Today.DateOfWeek.ToString() = "Saturday" Then   'Do something on Saturday End If

This will work perfectly, but it looks a bit of a mess. Why? You can answer this question by yourself after looking at the neater version using the Select Case statement, which would look like something like this:

Select Case DateTime.Today.DateOfWeek.ToString() Case "Sunday":     'Do something on Sunday Case "Monday":     'Do something on Monday Case "Tuesday":    'Do something on Tuesday Case "Wednesday":  'Do something on Wednesday Case "Thursday":   'Do something on Thursday Case "Friday":     'Do something on Friday Case "Saturday":   'Do something on Saturday End Select

When you've got lots of possible branches, the Select Case statement is far neater. It doesn't really let you do anything that you can't do with lots of ElseIfs, but it is often a much easier and tidier way to do things. Other conditions may exist in every ElseIf, while there is only one condition in a Select Case evaluation and different values of the condition evaluated will fire different blocks of code to execute.

Important

The condition in the Case statement must evaluate to Integer data type or to an enumeration data type, and the code will become more elegant and readable if you can use a Case statement appropriately.

Try It Out—Select Case in Action

Let's take another look at test results. In this example, we'll let users select a score from a list, and then tell them what grade they got. Since there are lots of possible grades, this will look far neater using the Select Case statement than using an If block.

  1. Create a new ASP.NET Page named SelectCase.aspx in Web Matrix.

  2. Drag a DropDownList control and a Label control on a new line below it.

  3. Erase the Text property of the Label control, and change the AutoPostBack property of DropDownList control to True in the Properties window.

  4. Now we need to add some items to the list. If you click on the Items property of DropDownList control in the Properties pane, you will see a button with 3 dots in it. Click on this button and the ListItem Collection Editor will appear:

    click to expand

  5. Click the Add Button and then enter - Please select a range of score - into the Text item on the right. Then, set the Value item the 0.

  6. Now, we need to do a similar thing for the score ranges. Click the Add button to create another 5 items. Set the Text properties to 0 - 20, 21 - 40, 41 - 60, 61 80, and 81 - 100 with Value properties of 1, 2, 3, 4, and 5 respectively. The Text is used for displaying the text of an item, while Value is used for telling our program which item the user chose – we'll see this in action very soon.

  7. Finally, mark the Selected property of the first item to True, so that users see the instructions when the page first loads:

    click to expand

    Finally, hit the OK button to accept the changes you have made in the dialog.

  8. Now we move on to the code. Double-click the DropDownList control to add a DropDownList1_SelectedIndexChanged event handler to the code and modify it as follows:

    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)  Select Case DropDownList1.SelectedItem.Value  Case 0: Label1.Text = String.Empty  ' Reset the Label1.Text  Case 1: Label1.Text = "You got Grade E!"  Case 2: Label1.Text = "You got Grade D!"  Case 3: Label1.Text = "You got Grade C!"  Case 4: Label1.Text = "You got Grade B!"  Case 5: Label1.Text = "You got Grade A!"  End Select End Sub

  9. Run the page by using the usual methods and try selecting any item from the DropDownList control:

    click to expand

    Depending on which range from the DropDownList control you chose, you will see an appropriate message.

How It Works

Each item in the DropDownList has a Text and a Value property. The Text property is the visual text on screen and the Value property is associated with an item in the DropDownList, which is usually a numeric value corresponding to the friendlier (but lengthier) text being displayed. By selecting items in the DropDownList, the value of the selected item in the DropDownList changes too. Then, while testing the value of the selected item in the list, we know which item was selected, and thus the corresponding block of code will be executed. Moreover, like Else in the If statement, there is a Case Else condition that executes if a value fails to match with any of the Case conditions.

What About that AutoPostBack Property?

When you make a client side event for a server control, for example, change a selected item in DropDownList or click on a Button, it will submit the current document to itself – that's called postback. By raising a postback event, selected actions taken on the client would fire a request back to the server, and thus the corresponding server-side event handler will be executed.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net