Condividi tramite


Determinazione del tipo di oggetto (Visual Basic)

Le variabili oggetto generiche (ovvero le variabili dichiarate come Object) possono contenere oggetti da qualsiasi classe. Quando si usano variabili di tipo Object, potrebbe essere necessario eseguire azioni diverse in base alla classe dell'oggetto, ad esempio alcuni oggetti potrebbero non supportare una determinata proprietà o metodo. Visual Basic offre due mezzi per determinare il tipo di oggetto archiviato in una variabile oggetto: la TypeName funzione e l'operatore TypeOf...Is .

TypeName e TypeOf... È

La TypeName funzione restituisce una stringa ed è la scelta migliore quando è necessario archiviare o visualizzare il nome della classe di un oggetto, come illustrato nel frammento di codice seguente:

Dim Ctrl As Control = New TextBox
MsgBox(TypeName(Ctrl))

L'operatore TypeOf...Is è la scelta migliore per testare il tipo di un oggetto, perché è molto più veloce rispetto a un confronto di stringhe equivalente usando TypeName. Il frammento di codice seguente usa TypeOf...Is all'interno di un'istruzione If...Then...Else :

If TypeOf Ctrl Is Button Then
    MsgBox("The control is a button.")
End If

Una parola di cautela è dovuta qui. L'operatore TypeOf...Is restituisce True se un oggetto è di un tipo specifico o è derivato da un tipo specifico. Quasi tutto ciò che si fa con Visual Basic include oggetti, che includono alcuni elementi non normalmente considerati come oggetti, ad esempio stringhe e interi. Questi oggetti sono derivati da e ereditano metodi da Object. Quando viene passato un oggetto Integer e valutato con Object, l'operatore TypeOf...Is restituisce True. Nell'esempio seguente viene segnalato che il parametro InParam è sia un Object che un Integer.

Sub CheckType(ByVal InParam As Object)
    ' Both If statements evaluate to True when an
    ' Integer is passed to this procedure.
    If TypeOf InParam Is Object Then
        MsgBox("InParam is an Object")
    End If
    If TypeOf InParam Is Integer Then
        MsgBox("InParam is an Integer")
    End If
End Sub

Nell'esempio seguente vengono utilizzati sia TypeOf...Is che TypeName per determinare il tipo di oggetto passato nell'argomento Ctrl . La TestObject procedura chiama ShowType con tre tipi diversi di controlli.

Per eseguire l'esempio

  1. Creare un nuovo progetto di applicazione Windows e aggiungere un controllo Button, un controllo CheckBox e un controllo RadioButton al modulo.

  2. Dalla pulsante del modulo, chiama la TestObject procedura.

  3. Aggiungere il codice seguente al modulo:

    Sub ShowType(ByVal Ctrl As Object)
        'Use the TypeName function to display the class name as text.
        MsgBox(TypeName(Ctrl))
        'Use the TypeOf function to determine the object's type.
        If TypeOf Ctrl Is Button Then
            MsgBox("The control is a button.")
        ElseIf TypeOf Ctrl Is CheckBox Then
            MsgBox("The control is a check box.")
        Else
            MsgBox("The object is some other type of control.")
        End If
    End Sub
    
    Protected Sub TestObject()
        'Test the ShowType procedure with three kinds of objects.
        ShowType(Me.Button1)
        ShowType(Me.CheckBox1)
        ShowType(Me.RadioButton1)
    End Sub
    

Vedere anche