Domanda

Nello spirito del c # domanda ..

Quali sono le istruzioni equivalenti per confrontare i tipi di classe in VB.NET?

È stato utile?

Soluzione

Stai cercando qualcosa come TypeOf ? Funziona solo con tipi di riferimento, non int / etc.

If TypeOf "value" Is String Then
     Console.WriteLine("'tis a string, m'lord!")

O vuoi confrontare due diverse istanze di variabili? Funziona anche con i tipi di riferimento:

Dim one As Object = "not an object"
Dim two As Object = "also not an object, exactly"
Dim three as Object = 3D

If one.GetType.Equals( two.GetType ) Then WL("They are the same, man")
If one.GetType Is two.GetType then WL("Also the same")
If one.GetType IsNot three.GetType Then WL("but these aren't")

Puoi anche usare gettype () in questo modo, se non stai usando due oggetti:

If three.GetType Is gettype(integer) then WL("is int")

Se vuoi vedere se qualcosa è una sottoclasse di un altro tipo (e si trova in .net 3.5):

If three.GetType.IsSubclassOf(gettype(Object)) then WL("it is")

Ma se vuoi farlo nelle versioni precedenti, devi capovolgerlo (strano da guardare) e usare:

If gettype(Object).IsAssignableFrom(three.GetType) Then WL("it is")

Tutte queste compilazioni in SnippetCompiler , quindi vai a DL se non hai esso.

Altri suggerimenti

TypeOf obj Is MyClass

L'equivalente VB alla domanda collegata è quasi identico:

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top