Share via


Operador AndAlso (Visual Basic)

Executa a conjunção lógica circuiting de-de curto em duas expressões.

result = expression1 AndAlso expression2

Parts

Term

Definition

result

Required. Any Boolean expression. O resultado é o Boolean o resultado da comparação de duas expressões.

expression1

Required. Any Boolean expression.

expression2

Required. Any Boolean expression.

Comentários

A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure calls.

Se as duas expressões são True, result é True. The following table illustrates how result is determined.

If expression1 is

And expression2 is

The value of result is

True

True

True

True

False

False

False

(not evaluated)

False

Um uso o AndAlso operador é teste a existência de uma instância de objeto antes de tentar o acesso um de seus membros. A seguinte linha de código ilustra isso.

If newObject AndAlso newObject.initFinished Then

O acesso para o initFinishedapropriedade no código anterior linha poderia lançar um NullReferenceExceptionexceção se o newObjectvariável não tinha uma instância do objeto atribuída ao proprietário. No entanto, o AndAlso operador faz o compilador ignorar o acesso a initFinished se newObject é Nothing, pois Nothing for avaliada como False.

Data Types

The AndAlso operator is defined only for the Tipo de dados booleanos (Visual Basic). Visual Basic converts each operand as necessary to Boolean and performs the operation entirely in Boolean. If you assign the result to a numeric type, Visual Basic converts it from Boolean to that type. This could produce unexpected behavior. Por exemplo, 5 AndAlso 12 resulta em –1 quando convertido em Integer.

Overloading

The Operador And (Visual Basic) and the Operador IsFalse (Visual Basic) can be overloaded, which means that a class or structure can redefine their behavior when an operand has the type of that class or structure. Overloading the And and IsFalse operators affects the behavior of the AndAlso operator. If your code uses AndAlso on a class or structure that overloads And and IsFalse, be sure you understand their redefined behavior. For more information, see Procedimentos de operador (Visual Basic).

Exemplo

The following example uses the AndAlso operator to perform a logical conjunction on two expressions. O resultado é um Boolean valor que representa o fato de toda a expressão de conjoined é verdadeiro. If the first expression is False, the second is not evaluated.

Dim a As Integer = 10
Dim b As Integer = 8
Dim c As Integer = 6
Dim firstCheck, secondCheck, thirdCheck As Boolean
firstCheck = a > b AndAlso b > c
secondCheck = b > a AndAlso b > c
thirdCheck = a > b AndAlso c > b

The preceding example produces results of True, False, and False, respectively. In the calculation of secondCheck, the second expression is not evaluated because the first is already False. However, the second expression is evaluated in the calculation of thirdCheck.

A exemplo a seguir mostra um Function procedimento que procura por um determinado valor entre os elementos de uma matriz. Se matriz estiver vazia, ou se o comprimento da matriz foi excedido, o While demonstrativo não não teste o elemento de matriz com relação ao valor de pesquisa.

Public Function findValue(ByVal arr() As Double, 
    ByVal searchValue As Double) As Double
    Dim i As Integer = 0
    While i <= UBound(arr) AndAlso arr(i) <> searchValue
        ' If i is greater than UBound(arr), searchValue is not checked.
        i += 1
    End While
    If i > UBound(arr) Then i = -1
    Return i
End Function

Consulte também

Referência

Operadores lógicos/bit a bit (Visual Basic)

Precedência de operadores no Visual Basic

Operadores listados por Funcionalidade (Visual Basic)

Operador And (Visual Basic)

Operador IsFalse (Visual Basic)

Conceitos

Operadores lógicos e bit a bit no Visual Basic