【发布时间】:2010-09-10 12:45:05
【问题描述】:
如何在 VB 6 中检查对象的类型 - 除了“TypeName”之外还有其他方法吗,因为用“TypeName”检查它是不可行的,我期待像 QuichWatch 窗口这样的东西。
【问题讨论】:
标签: vb6
如何在 VB 6 中检查对象的类型 - 除了“TypeName”之外还有其他方法吗,因为用“TypeName”检查它是不可行的,我期待像 QuichWatch 窗口这样的东西。
【问题讨论】:
标签: vb6
对于对象变量,使用TypeOf ... Is:
If TypeOf VarName Is TypeName Then
''# ...
End If
例如:
Dim fso As New Scripting.FileSystemObject
If TypeOf fso Is Scripting.FileSystemObject Then
Debug.Print "Yay!"
End If
【讨论】:
只是添加到@Tomalak 的答案...如果对象变量尚未实例化,则使用 TypeOf 进行测试将导致运行时错误。另请注意,该类可能实现接口,例如
Dim fs As Scripting.FileSystemObject
On Error Goto Err_Handler
If TypeOf fs Is Scripting.FileSystemObject Then
Debug.Print "[Won't get here]"
End If
Err_Handler:
If Err.Number <> 0 Then
Debug.Print "Oops, error when fs Is Nothing"
End If
On Error Resume Next
Set fs = New Scripting.FileSystemObject
If TypeOf fs Is Scripting.FileSystemObject Then
Debug.Print "Is a FileSystemObject"
End If
If TypeOf fs Is IFileSystem Then
Debug.Print "Implements IFileSystem "
End If
【讨论】:
试试这个。
dim obj as object
for each obj in me
debug.print TypeName(obj)
next
【讨论】: