Decision Structures

Select Case Decision Structures

 

Select Case Decision Structures

There is another way to control the execution of program statements in your application--Select Case decision structures.

A Select Case structure is similar to an If…Then…ElseIf structure, but it is more efficient.

They also help make your code more readable.

 

The syntax for Select Case

Select Case variable

Case Value1

program statements executed if value1 matches variable

Case Value2

program statements executed if value2 matches variable

Additional Case Variables as needed

End Select

 

Syntax of Select Case cont…

A Select Case structure begins with the Select Case keywords and ends with the End Select keywords.

You replace the variable with the variable, property, or other expression that is to be the key value, or test case, for the structure.

If one of the values matches the variable, the statements below its Case clause are executed.

 

A Select Case Example

Select Case Tax

Case 3000

Label1.Caption="You pay 15%"

Case 10000

Label1.Caption="You pay 30%"

Case Else

Label1.Caption="Give me all your money!"

End Select

 

A Select Case Example

Select case also allows you to include a range of operators (=, < , >, < >, <=).

To use these operators you need to use the Keyword "Is."

Example:

Select Case Tax

Case Is < 3000

Label1.Caption="Get a real job"

 

Processing a list box--Program Example

Let’s create a program that use Select case to process the selection from a list box.

Start a new Application

Draw a list box, a label box to display the results, and a command button to execute the program.

 

Add Items to the List Box

The following code adds items to the list box function. The code goes under the Sub Form_Load proc

Private Sub Form_Load()

Combo1.AddItem "England"

Combo1.AddItem "Germany"

Combo1.AddItem "Spain"

Combo1.AddItem "Italy"

List1.AddItem "England"

List1.AddItem "Germany"

List1.AddItem "Spain"

List1.AddItem "Italy"

 

Process the Click

Finally we need to process the user’s click.

This procedure goes under the Private Sub List1_Click()

Label3.Caption = List1.Text

Select Case Combo1.ListIndex

Case 0

Label4.Caption = "Hello, programmer"

Case 1

Label4.Caption = "Hallo, Programmierer"

Case 2

Label4.Caption = "Hola, programador"

Case 3

Label4.Caption = "Ciao, programmatori"

End Select

 

Finding and Correcting Errors

Debugging

Finding errors in your programs and correcting them, known as debugging, is a time consuming process in any programming language.

VB provides us with several tools for helping us with the debugging process.

Before we get to those tools let’s discuss the types of errors you may get.

 

Types of Errors

There are three type of errors that can occur in a VB program. They are:

 

A syntax error (or compiler error) which is a programming mistake that violates the rules of VB. VB points out several types of these while you are typing your code and won’t let you run the program until they are corrected.

Errors cont...

Runtime errors are a mistake that causes a program to stop unexpectedly during execution. Runtime error occur when an outside event or an undiscovered syntax error forces a program to stop while it is running. A misspelled filename in a LoadPicture function or an open floppy drive are conditions that can produce runtime errors.

Errors cont...

A logic error is a human error, a programming mistake that makes the program code produce the wrong result. Most debugging efforts are focused on tracking down logic errors introduced by the programmer.

Break Mode

Break mode allows you to track down errors by examining your code line by line and viewing the variables or properties as they change.

It is helpful to open the Debug toolbar when you use Break Mode to give quick access to many of the tools you will use.