Interaction.Switch(Object[]) Método
Definição
Avalia uma lista de expressões e retorna um valor Object correspondente à primeira expressão na lista, que é True.Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.
public:
static System::Object ^ Switch(... cli::array <System::Object ^> ^ VarExpr);
public static object? Switch (params object?[]? VarExpr);
public static object Switch (params object[] VarExpr);
static member Switch : obj[] -> obj
Public Function Switch (ParamArray VarExpr As Object()) As Object
Parâmetros
- VarExpr
- Object[]
Obrigatórios.Required. Matriz de parâmetros Object.Object parameter array. Deve ter um número par de elementos.Must have an even number of elements. Você pode fornecer uma lista de variáveis Object ou expressões separadas por vírgulas ou uma matriz unidimensional de elementos Object.You can supply a list of Object variables or expressions separated by commas, or a single-dimensional array of Object elements.
Retornos
Avalia uma lista de expressões e retorna um valor Object correspondente à primeira expressão na lista, que é True.Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.
Exceções
O número de argumentos é ímpar.Number of arguments is odd.
Exemplos
O exemplo a seguir usa a Switch função para retornar o nome de um idioma que corresponde ao nome de uma cidade.The following example uses the Switch function to return the name of a language that matches the name of a city. Isso requer que Option Strict seja Off .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
Como o System.Diagnostics namespace também contém uma classe chamada Switch , uma chamada para a Switch função deve ser qualificada com o Microsoft.VisualBasic namespace.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.
Comentários
O argumento fornecido para VarExpr consiste em valores e expressões emparelhadas.The argument supplied to VarExpr consists of paired expressions and values. A Switch função avalia as expressões de números ímpares do índice mais baixo para o mais alto em VarExpr e retorna o valor de número par associado à primeira expressão avaliada como True .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. Por exemplo, se VarExpr(0) é True , Switch retorna VarExpr(1) e se VarExpr(0) é, False mas VarExpr(2) é True , Switch retorna VarExpr(3) e assim por diante.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.
Se você não fornecer o VarExpr argumento, Switch retornará Nothing .If you do not supply the VarExpr argument, Switch returns Nothing.
Observação
As expressões na lista de argumentos podem incluir chamadas de função.The expressions in the argument list can include function calls. Como parte da preparação da lista de argumentos para a chamada para Switch , o compilador Visual Basic chama cada função em cada expressão.As part of preparing the argument list for the call to Switch, the Visual Basic compiler calls every function in every expression. Isso significa que você não poderá contar com uma função específica que não será chamada se uma expressão anterior na lista de argumentos for True .This means that you cannot rely on a particular function not being called if an expression earlier in the argument list is True.