当前位置:网站首页>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 :
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 FunctionBased 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 FunctionTesting 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 SubOutput: Output :
v1: Variant/Integeri: Integerv2: Variant/RangeOb: RangeR: RangeThis 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 FunctionTest: test :
Sub test() Dim R As Range Set R = Range("A1") Debug.Print IsTypeObject(R)End SubBut 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边栏推荐
- 詳解機器翻譯任務中的BLEU
- Readonly read only
- Pass parent component to child component: props
- I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
- Chinese and English instructions prosci LAG-3 recombinant protein
- Talk about seven ways to realize asynchronous programming
- How to * * labelimg
- PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
- 1089: highest order of factorial
- 毕设-基于SSM大学生兼职平台系统
猜你喜欢

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met

BGP experiment (1)

计算机服务中缺失MySQL服务

1141_ SiCp learning notes_ Functions abstracted as black boxes

1140_ SiCp learning notes_ Use Newton's method to solve the square root

English translation is too difficult? I wrote two translation scripts with crawler in a rage

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp

Bi she - college student part-time platform system based on SSM

一、Go知识查缺补漏+实战课程笔记 | 青训营笔记

Lm11 reconstruction of K-line and construction of timing trading strategy
随机推荐
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
PostgreSQL source code (60) transaction system summary
抽丝剥茧C语言(高阶)数据的储存+练习
Bindingexception exception (error reporting) processing
电商常规问题part1
Advanced practice of C language (high level) pointer
"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr
Deep learning Flower Book + machine learning watermelon book electronic version I found
Simple example of ros2 planning system plansys2
外包干了三年,废了...
弹性布局(二)
How to * * labelimg
Example of Pushlet using handle of Pushlet
$parent (get parent component) and $root (get root component)
云备份项目
Explain Bleu in machine translation task in detail
Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
Blue Bridge Cup Netizen age (violence)
Communication of components
Torefs API and toref API
