Return Select Case Branching Statements

56 End If If expression is True , only the statements in the If clause are executed. If expression is False , only the statements in the Else clause are executed. Finally, a sequence of expressions can be evaluated by including one or more ElseIf clauses, as shown here: If expression Then statements ElseIf expression Then statements ElseIf expression Then statements Else statements End If The first If or ElseIf clause whose expression evaluates to True will have its statements executed. Statements in subsequent ElseIf clauses will not be executed, even if their corresponding expressions are also True . If none of the expressions evaluate to True , the statements in the Else clause will be executed. The Else clause can be omitted if desired.

2.13.2.5 RaiseEvent

The RaiseEvent statement fires the given event. After the event has been fired to all listeners, execution continues with the first statement following the RaiseEvent statement. See Sect ion 2.20 later in this chapter for more information.

2.13.2.6 Return

The Return statement exits a function and provides a return value to the caller of the function. Execution continues with the first statement following the statement that called the function. Here is an example: Public Shared Function MyFactorialByVal value As Integer As Integer Dim retval As Integer = 1 Dim i As Integer For i = 2 To value retval = i Next Return retval End Function Another way to return a value to the caller of the function is to assign the value to the function name and then simply drop out of the bottom of the function. This is how it was done in Visual Basic 6 and can still be done in Visual Basic .NET. Here is an example: Public Shared Function MyFactorialByVal value As Integer As Integer Dim retval As Integer = 1 Dim i As Integer For i = 2 To value retval = i Next MyFactorial = retval End Function 57 In Visual Basic 6, the Return statement was used to return execution to the statement following a GoSub statement. In Visual Basic .NET, the GoSub statement no longer exists, and the Return statement is now used as described here.

2.13.2.7 Select Case

The Select Case statement chooses a block of statements to execute based on some value. For example: Select Case strColor Case red ... Case green ... Case blue ... Case yellow ... Case Else ... End Select If strColor in this example contains blue , only the statements in the Case blue clause are executed. If none of the Case clauses matches the value in the Select Case statement, the statements in the Case Else clause are executed. If more than one Case clause matches the given value, only the statements in the first matching Case clause are executed. Case statements can include multiple values to be matched against the value given in the Select Case statement. For example: Case red, green, blue, strSomeColor This case will be matched if the value in the Select Case statement is red , green , blue , or the value contained in strSomeColor . The To keyword can be used to match a range of values, as shown here: Case apples To oranges This Case statement matches any string value that falls alphabetically within this range. The Is keyword can be used for matching an open-ended range: Case Is oranges Dont confuse this use of the Is keyword with the Is comparison operator.

2.13.3 Iteration Statements