当前位置:网站首页>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边栏推荐
- 1089: highest order of factorial
- 1141_ SiCp learning notes_ Functions abstracted as black boxes
- Causes and solutions of oom (memory overflow)
- 一、Go知识查缺补漏+实战课程笔记 | 青训营笔记
- Communication of components
- 四、高性能 Go 语言发行版优化与落地实践 青训营笔记
- 关于二进制无法精确表示小数
- 计算机服务中缺失MySQL服务
- How do I get the last part of a string- How to get the last part of a string?
- 毕设-基于SSM大学生兼职平台系统
猜你喜欢
外包干了三年,废了...
毕设-基于SSM大学生兼职平台系统
1140_ SiCp learning notes_ Use Newton's method to solve the square root
transform-origin属性详解
抽絲剝繭C語言(高階)數據的儲存+練習
外包幹了三年,廢了...
How can a 35 year old programmer build a technological moat?
抽丝剥茧C语言(高阶)数据的储存+练习
URP - shaders and materials - light shader lit
1141_ SiCp learning notes_ Functions abstracted as black boxes
随机推荐
A concurrent rule verification implementation
计算机服务中缺失MySQL服务
Le Service MySQL manque dans le service informatique
在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
Leetcode-543. Diameter of Binary Tree
Make a bat file for cleaning system garbage
Initial experience of teambiion network disk (Alibaba cloud network disk)
Bi she - college student part-time platform system based on SSM
记一个并发规则验证实现
Abnova immunohistochemical service solution
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
C language (high-level) data storage + Practice
URP - shaders and materials - light shader lit
About binary cannot express decimals accurately
[Linux] process control and parent-child processes
1089: highest order of factorial
Blue Bridge Cup Birthday candles (violence)
1141_ SiCp learning notes_ Functions abstracted as black boxes
Circulating tumor cells - here comes abnova's solution
transform-origin属性详解