Switch Function


Switch Function

Class

Microsoft.VisualBasic.Interaction

Syntax

     Dim result As Object = Switch(expr_1, value_1[, _        expr_2, value_2[..., expr_n, value_n]]) 


expr_1 to expr_n (required; Boolean)

A number of expressions to be evaluated.


value_1 to value_n (required; Object)

A number of expressions from which one is returned if the associated expression is the first one to evaluate to true.

Instead of using a comma-delimited list of expressions and values, all expressions and values can be stored in a ParamArray object, with elements in the same order that they would appear in the argument list.

Description

The Switch function evaluates a list of expressions and, on finding the first expression to evaluate to TRue, returns an associated value.

Usage at a Glance

  • At least one expression/value pair must be included, and they must always appear in pairs.

  • Expressions are evaluated from left to right.

  • If none of the expressions is TRue, the Switch function returns Nothing.

  • The parameters can be variables, constants, literals, expressions, or function calls. Each value parameter may be of a different type; the return value will be of type Object.

  • All parameters are fully evaluated before they are considered as conditions or results for the Switch function. If they contain function calls, those functions will be called, even in the items that are not returned by the function. For instance, in the statement:

         result = Switch(useTempFile, ProcessFile(tempFileName), _        True, ProcessFile(mainFileName)) 

    both calls to ProcessFile will always be performed, regardless of the value of useTempFile. However, at most, only one return value from among the function calls will be returned from the Switch function, and possibly none.

  • This function does not support named arguments.

  • By providing all parameters in the form of a ParamArray, the list of values can be expanded or contracted programmatically at runtime.

Example

This example returns a string based on a selection of numeric ranges.

     rangeText = Switch(currentValue < 0, "Negative", _        currentValue > 0, "Positive", True, "Neutral") 

See Also

Choose Function




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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