我实际上试图检查程序中是否定义了 variable
。
我是通过使用 exception handling
技术完成的,如下所示,
private sub IsTestVarDefined() as boolean
try
dim xDummy = AnObject.TestVar 'Where AnObject is using the type Object
return true
catch
return false
end try
end sub
是否有任何简单的解决方案可以实现这一目标。?或者这可以实现。?
如果我使用 javascript 编程,那么我会这样做,
if(TypeOf Testvar === "undefined") { ... }
我一直在 vb.net 中寻找与上述方法非常相似的方法。
我的案例示例图片:
Public Class Class1
public Dim xVar as integer = 0
End Class
Public Class Class2
public Dim xAnotherVar as integer = 0
End Class
Public Class SomeOtherClass
Dim xObj as Object = New Class2
'Now i want to check whether the xObj is having xVar or Not?
End Class
附加说明:
@Damien_The_Unbeliever 解决方案不返回任何内容,即使类型转换对象具有该成员。
'Evaluated by using the above case i given
?xObj.GetType().GetProperty("xAnotherVar")
Nothing
最佳答案
您可以使用反射:
Return AnObject.GetType().GetProperty("TestVar") IsNot Nothing
关于.net - 测试变量是否存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19396456/