How to check type of object in VB 6 - Is there any method other than 'TypeName'

て烟熏妆下的殇ゞ 提交于 2020-01-09 05:40:18

问题


How to check type of object in VB 6 - Is there any method other than 'TypeName' because its not feasible to check it witrh 'TypeName' I am expecting something like QuichWatch window.


回答1:


For object variables, use TypeOf ... Is:

If TypeOf VarName Is TypeName Then
  ''# ...
End If

For example:

Dim fso As New Scripting.FileSystemObject

If TypeOf fso Is Scripting.FileSystemObject Then
  Debug.Print "Yay!"
End If



回答2:


Just to add to @Tomalak's answer... If the object variable has not been instantiated then testing with TypeOf will cause a run time error. Also note the class may implement interfaces e.g.

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



回答3:


try this one.

dim obj as object
for each obj in me
   debug.print TypeName(obj)
next


来源:https://stackoverflow.com/questions/3684693/how-to-check-type-of-object-in-vb-6-is-there-any-method-other-than-typename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!