Export (0) Print
Expand All

Interaction.Switch Method

Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static Object Switch(
	params Object[] VarExpr
)

Parameters

VarExpr
Type: System.Object[]

Required. Object parameter array. Must have an even number of elements. You can supply a list of Object variables or expressions separated by commas, or a single-dimensional array of Object elements.

Return Value

Type: System.Object
Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.

For more detailed information, see the Visual Basic topic Switch Function.

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.

NoteNote:

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.

Function matchLanguage(ByVal cityName As String) As String 
    Return CStr(Microsoft.VisualBasic.Switch( _
        cityName = "London", "English", _
        cityName = "Rome", "Italian", _
        cityName = "Paris", "French"))
End Function

Because the System.Diagnostics namespace also contains a class called Switch, a call to the Switch function must be qualified with the Microsoft.VisualBasic namespace.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft