当前位置:网站首页>Determining the full type of a variable

Determining the full type of a variable

2022-07-07 07:34:00 Bustling city

problem :

By the full type of a variable I mean the sort of information that you get in the immediate window: adopt Complete type The variable of , I mean the type of information you get in the instant window :

 Enter the image description here

I would like to determine the type information dynamically using VBA. I want to use VBA Dynamically determine type information .The function TypeName() doesn't do what I want since it returns the subtype of a variant and doesn't distinguish between eg a variant variable holding a range, an object variable holding a range, and a range variable holding a range. function TypeName() Can't do what I want , Because it returns the subtypes , And do not distinguish, for example, variables that hold ranges , The object variable of holding range and the range variable of holding range .

As a preliminary step, I wrote a function which detects if a variant is passed to it. As a preliminary step , I wrote a function , Used to detect whether a variable is passed to it .It works by exploiting pass-by-reference semantics. It works by using passing reference semantics .The code does things with its argument that can only be done with a variant and will thus trigger an error if the passed variable isn't actually a variant: The operations performed by the code with its parameters can only be done through variables , So if the variable passed is not actually a variable , Will trigger an error :

Function IsVariant(var As Variant) As Boolean    Dim temp As Variant    Dim isVar As Boolean    If IsObject(var) Then        Set temp = var    Else        temp = var    End If    On Error Resume Next        Set var = New Collection        var = "test"        If Err.Number > 0 Then            isVar = False        Else            isVar = True        End If    On Error GoTo 0    If IsObject(temp) Then        Set var = temp    Else        var = temp    End If    IsVariant = isVarEnd Function

Based on this, I wrote: Based on this , I wrote :

Function FullType(var As Variant) As String    If IsVariant(var) Then        FullType = "Variant/" & TypeName(var)    Else        FullType = TypeName(var)    End IfEnd Function

Testing code: Test code :

Sub TestTypes()    Dim R As Range    Dim Ob As Object    Dim i As Integer    Dim v1 As Variant    Dim v2 As Variant    v1 = 10    i = 10    Set v2 = Range("A1")    Set Ob = Range("A2")    Set R = Range("A3")    Debug.Print "v1: " & FullType(v1)    Debug.Print "i: " & FullType(i)    Debug.Print "v2: " & FullType(v2)    Debug.Print "Ob: " & FullType(Ob)    Debug.Print "R: " & FullType(R)  End Sub

Output: Output :

v1: Variant/Integeri: Integerv2: Variant/RangeOb: RangeR: Range

This is almost what I want -- but doesn't distinguish between an object variable holding a range and a range variable holding a range. This is almost what I want - However, there is no distinction between object variables that maintain a range and range variables that maintain a range .I've tried to write a function called IsTypeObject which works similarly to IsVariant but can't seem to get it to work: I once tried to write a program named IsTypeObject Function of , How it works IsVariant similar , But it doesn't seem to work :

Function IsTypeObject(var As Variant) As Boolean    Dim temp As Variant    Dim isGeneric As Boolean    If (Not IsObject(var)) Or IsVariant(var) Then        IsTypeObject = False        Exit Function    End If    Set temp = var    On Error Resume Next        Set var = New Collection        Set var = ActiveWorkbook        If Err.Number > 0 Then            isGeneric = False        Else            isGeneric = True        End If    On Error GoTo 0    Set var = temp    IsTypeObject = isGenericEnd Function

Test: test :

Sub test()    Dim R As Range    Set R = Range("A1")    Debug.Print IsTypeObject(R)End Sub

But this prints True even though I would think that the same pass-by-reference semantics which makes IsVariant work should also make IsTypeObject work (you can't assign a collection to a range). But this will print True Even if I think it makes IsVariant Work the same pass-by-reference Semantics should also make IsTypeObject Work ( You cannot assign a set to a range ).I've tried various tweaks but can't seem to distinguish between the generic object variables and specific object variables such as range variables. I tried various adjustments , But it seems impossible to distinguish between general object variables and specific object variables , Such as range variable .

So -- any ideas for how to dynamically get the full type of a variable? that - Any idea about how to dynamically obtain the complete type of variables ?(The motivation is as part of a debug-log utility)( Motivation is part of the debug logging utility )


Solution :

Reference resources : https://stackoom.com/en/question/2IPhL
原网站

版权声明
本文为[Bustling city ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130653518385.html