Operatore Is (Visual Basic)

Confronta due variabili di riferimento a oggetti.

Sintassi

result = object1 Is object2

Parti

result
Obbligatorio. Qualsiasi valore Boolean.

object1
Obbligatorio. Qualsiasi nome Object.

object2
Obbligatorio. Qualsiasi nome Object.

Osservazioni:

L'operatore Is determina se due riferimenti a oggetti fanno riferimento allo stesso oggetto. Tuttavia, non esegue confronti tra valori. Se object1 e object2 fanno entrambi riferimento alla stessa istanza dell'oggetto, result è True; in caso contrario, result è False.

Nota

La parola chiave Is viene usata anche nell'istruzione Select... Case.

Esempio

Nell'esempio seguente viene usato l'operatore Is per confrontare coppie di riferimenti a oggetti. I risultati vengono assegnati a un valore Boolean che indica se i due oggetti sono identici.

Dim myObject As New Object
Dim otherObject As New Object
Dim yourObject, thisObject, thatObject As Object
Dim myCheck As Boolean
yourObject = myObject
thisObject = myObject
thatObject = otherObject
' The following statement sets myCheck to True.
myCheck = yourObject Is thisObject
' The following statement sets myCheck to False.
myCheck = thatObject Is thisObject
' The following statement sets myCheck to False.
myCheck = myObject Is thatObject
thatObject = myObject
' The following statement sets myCheck to True.
myCheck = thisObject Is thatObject

Come illustrato nell'esempio precedente, è possibile usare l'operatore Is per testare sia gli oggetti associati in anticipo che in ritardo.

Usare l'operatore TypeOf con l'operatore Is

L'operatore Is può essere usato anche con la parola chiave TypeOf per creare un'espressione TypeOf...Is, che verifica se una variabile oggetto è compatibile con un tipo di dati. Ad esempio:

If TypeOf sender Is Button Then

Vedi anche