当前位置:网站首页>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 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边栏推荐
- Advanced level of C language (high level) pointer
- SQLMAP使用教程(四)实战技巧三之绕过防火墙
- Nesting and splitting of components
- 按键精灵脚本学习-关于天猫抢红包
- My ideal software tester development status
- Interviewer: what development models do you know?
- "Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr
- [cloud native] how to give full play to memory advantage of memory database
- [semantic segmentation] - multi-scale attention
- 95后CV工程师晒出工资单,狠补了这个,真香...
猜你喜欢
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
Communication between non parent and child components
弹性布局(二)
Circulating tumor cells - here comes abnova's solution
Composition API premise
Flexible layout (II)
三、高质量编程与性能调优实战 青训营笔记
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
How to * * labelimg
The currently released SKU (sales specification) information contains words that are suspected to have nothing to do with baby
随机推荐
Causes and solutions of oom (memory overflow)
抽絲剝繭C語言(高階)指針的進階
A concurrent rule verification implementation
1141_ SiCp learning notes_ Functions abstracted as black boxes
Lm11 reconstruction of K-line and construction of timing trading strategy
Calculus key and difficult points record part integral + trigonometric function integral
How to * * labelimg
Leetcode-206. Reverse Linked List
关于二进制无法精确表示小数
Outsourcing for three years, abandoned
在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
Bi she - college student part-time platform system based on SSM
07_ Handout on the essence and practical skills of text measurement and geometric transformation
Introduction to abnova's in vitro mRNA transcription workflow and capping method
95后CV工程师晒出工资单,狠补了这个,真香...
按键精灵采集学习-矿药采集及跑图
点亮显示屏的几个重要步骤
Model application of time series analysis - stock price prediction
Flexible layout (I)
二、并发、测试笔记 青训营笔记