【问题标题】:How to check type of object in VB 6 - Is there any method other than 'TypeName'如何在 VB 6 中检查对象的类型 - 除了“TypeName”之外还有其他方法吗
【发布时间】:2010-09-10 12:45:05
【问题描述】:

如何在 VB 6 中检查对象的类型 - 除了“TypeName”之外还有其他方法吗,因为用“TypeName”检查它是不可行的,我期待像 QuichWatch 窗口这样的东西。

【问题讨论】:

    标签: vb6


    【解决方案1】:

    对于对象变量,使用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
    

    【讨论】:

      【解决方案2】:

      只是添加到@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
      

      【讨论】:

        【解决方案3】:

        试试这个。

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

        【讨论】:

        • 谢谢,但是请检查我们必须修改现有代码的对象类型是否有任何方法,例如移动光标或对其进行快速查看,我可以看到它的类型
        猜你喜欢
        • 2015-12-08
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 2015-07-14
        相关资源
        最近更新 更多