当前位置:网站首页>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边栏推荐
- 面试官:你都了解哪些开发模型?
- Several important steps to light up the display
- 电商常规问题part1
- Circulating tumor cells - here comes abnova's solution
- js小练习----分时提醒问候、表单密码显示隐藏效果、文本框焦点事件、关闭广告
- Calculus key and difficult points record part integral + trigonometric function integral
- Project practice five fitting straight lines to obtain the center line
- 按键精灵采集学习-矿药采集及跑图
- Le Service MySQL manque dans le service informatique
- Summary of customer value model (RFM) technology for data analysis
猜你喜欢

At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it

Non empty verification of collection in SQL

Apache AB stress test

弹性布局(一)

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

Deep learning Flower Book + machine learning watermelon book electronic version I found

外包干了三年,废了...
![[semantic segmentation] - multi-scale attention](/img/2d/652681c08e484fe232b8fb91b7194e.jpg)
[semantic segmentation] - multi-scale attention

Leetcode-226. Invert Binary Tree
![[2022 CISCN]初赛 web题目复现](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 CISCN]初赛 web题目复现
随机推荐
ROS2规划系统plansys2简单的例子
Dynamics CRM server deployment - restore database prompt: the database is in use
毕设-基于SSM大学生兼职平台系统
Lm11 reconstruction of K-line and construction of timing trading strategy
UWB learning 1
Reflection (II)
1、 Go knowledge check and remedy + practical course notes youth training camp notes
URP - shaders and materials - simple lit
Chinese and English instructions prosci LAG-3 recombinant protein
MySQL service is missing from computer service
抽絲剝繭C語言(高階)指針的進階
FullGC问题分析及解决办法总结
Model application of time series analysis - stock price prediction
Convolutional neural network -- understanding of pooling
How to * * labelimg
[2022 CISCN]初赛 web题目复现
$refs: get the element object or sub component instance in the component:
Docker compose start redis cluster
抽丝剥茧C语言(高阶)指针进阶练习
【Liunx】进程控制和父子进程
