Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
The argument supplied to VarExpr consists of paired expressions and values. The Switch function evaluates the odd-numbered expressions from lowest to highest index in VarExpr, and returns the even-numbered value associated with the first expression that evaluates to True. For example, if VarExpr(0) is True, Switch returns VarExpr(1), and if VarExpr(0) is False but VarExpr(2) is True, Switch returns VarExpr(3), and so on.
If you do not supply the VarExpr argument, Switch returns Nothing.
The expressions in the argument list can include function calls. As part of preparing the argument list for the call to Switch, the Visual Basic compiler calls every function in every expression. This means that you cannot rely on a particular function not being called if an expression earlier in the argument list is True.
The following example uses the Switch function to return the name of a language that matches the name of a city. It requires that Option Strict be Off.
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)