JavaのinstanceofおよびisInstance()に相当するVBとは何ですか?
-
05-07-2019 - |
質問
c#質問。
VB.NETのクラスタイプを比較する同等のステートメントは何ですか?
解決
TypeOf
のようなものをお探しですか?これは参照型でのみ機能し、int / etcでは機能しません。
If TypeOf "value" Is String Then
Console.WriteLine("'tis a string, m'lord!")
または、変数の2つの異なるインスタンスを比較しますか? ref型でも機能します:
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")
2つのオブジェクトを使用していない場合は、このように gettype()
を使用することもできます。
If three.GetType Is gettype(integer) then WL("is int")
何かが別のタイプのサブクラス(および.net 3.5にある)かどうかを確認する場合:
If three.GetType.IsSubclassOf(gettype(Object)) then WL("it is")
しかし、以前のバージョンでそれをしたい場合は、それを裏返して(見た目が変)、次を使用する必要があります:
If gettype(Object).IsAssignableFrom(three.GetType) Then WL("it is")
これらはすべて SnippetCompiler でコンパイルされます。それ。
他のヒント
TypeOf obj Is MyClass
リンクされた質問に相当するVBはほとんど同じです:
Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
所属していません StackOverflow