Partilhar via


Como: Determinar se dois objetos são idênticos (Visual Basic)

Em Visual Basic, duas referências de variável são consideradas idênticas se os ponteiros são as mesmas, ou seja, se ambas as variáveis apontar para a mesma instância de classe na memória. Por exemplo, em umaplicativodo Windows Forms, convém fazer uma comparação para determinar se a instância atual (Me) é igual uma instância específica, como Form2.

Visual Basic provides two operators to compare pointers. The Operador Is (Visual Basic) returns True if the objects are identical, and the Operador IsNot (Visual Basic) returns True if they are not.

Determining if Two Objects Are Identical

To determine if two objects are identical

  1. Set up a Boolean expression to test the two objects.

  2. In your testing expression, use the Is operator with the two objects as operands.

    Is returns True if the objects point to the same class instance.

Determining if Two Objects Are Not Identical

Sometimes you want to perform an action if the two objects are not identical, and it can be awkward to combine Not and Is, for example If Not obj1 Is obj2. In such a case you can use the IsNot operator.

To determine if two objects are not identical

  1. Set up a Boolean expression to test the two objects.

  2. In your testing expression, use the IsNot operator with the two objects as operands.

    IsNot returns True if the objects do not point to the same class instance.

Exemplo

The following example tests pairs of Object variables to see if they point to the same class instance.

Dim objA, objB, objC As Object
objA = My.User
objB = New ApplicationServices.User
objC = My.User
MsgBox("objA different from objB? " & CStr(objA IsNot objB))
MsgBox("objA identical to objC? " & CStr(objA Is objC))

The preceding example displays the following output.

objA different from objB? True

objA identical to objC? True

Consulte também

Tarefas

Como: Determinar se dois objetos estão relacionados (Visual Basic)

Referência

Tipo de dados Object

Operador Is (Visual Basic)

Operador IsNot (Visual Basic)

Conceitos

Variáveis de objeto no Visual Basic

Valores de variável de objeto (Visual Basic)

Me, My, MyBase e MyClass no Visual Basic