Programming & Scripting Tutorials

Visual Basic: Simple Cases





Cases are extremely similar to If..else statements as they can make decisions if something or another happens; However unlike If..else statements cases only make decisions on one expression or dimension (for example the pupils score in a test).

This is a program which asks the user to input their score on a test, and then tells them how they did:

Dim mark As Integer
mark = Textbox1.Text

Select Case mark


Case Is >= 85
Label1.Text= "Excellent"

Case Is >= 70
 Label2.Text= "Good"

Case Is >= 60
 Label3.Text = "Above Average"

Case Is >= 50
Label4.Text= "Average"

Case Else
Label5.Text = "Need to work harder!"

End Select

We could also accomplish this via the to keyword in the select case:


Select Case mark

Case 0 to 49
Label1.Text = "Need to work harder!"

Case 50 to 59
Label2.Text = "Average"

Case 60 to 69
Label3.Text= "Above Average"

Case 70 to 84
Label4.Text = "Good"

Case Else
Label5.Text= "Excellent"

End Select

As you can see If..else statements are extremely useful, and so are cases; Here at dev-hq.co.uk we suggest a variety of both, as they are both very good and useful methods.

This Visual Basic tutorial was written by


Back to Visual Basic

Advertisement: