Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VarType() ALWAYS return 8204 for Arrays

In the VarType MSDN Microsoft documentation for VBScript's VarType function it says (With bold emphasis):

"Remarks The VarType function never returns the value for Array by itself. It is always added to some other value to indicate an array of a particular type. The value for Variant is only returned when it has been added to the value for Array to indicate that the argument to the VarType function is an array. For example, the value returned for an array of integers is calculated as 2 + 8192, or 8194. If an object has a default property, VarType (object) returns the type of its default property."

BUT

Code such as

Dim A,I1, I2, I3
I1 = 1
I2 = 2
I3 = 3
A = Array(I1,I2,I3)

Dim A2
A2 = Split("Test,Test,Test",",")

AT = VarType(A)
AT2 = VarType(A2)
IT1 = VarType(I1)
IT2 = VarType(I2)
IT3 = VarType(I3)

WScript.Echo IT1
WScript.Echo IT2
WScript.Echo IT3
WScript.Echo AT & " - 8192 = " & AT - 8192
WScript.Echo AT2 & " - 8192 = " & AT2 - 8192
WScript.Echo CStr(VarType(A(2)))

returns

2 
2
2
8204 - 8192 = 12 
8204 - 8192 = 12
2

I1 - I3 ALL return their proper vbInteger AND, when referenced individually in their array, ALSO return vbInteger, but the array insists it's an array of vbVariant.

Unless I'm COMPLETELY missing something here it seems that in SPITE of the documentation there is no way to create an array where the items are ALL of the SAME type and have VarType recognize it as as anything but an array of vbVariant.

I feel like this should already be a question but i came up empty searching through here so feel free to link if I missed the existing questions.

like image 500
TofuBug Avatar asked May 28 '15 16:05

TofuBug


People also ask

What is VarType?

VarType ( varname ) The required varnameargument is a Variant containing any variable except a variable of a user defined type. Return Values. Constant. Value.

How do I find data type in Excel VBA?

Then, open the VarType function. Now, enter the variable name as the argument of the VarType function. Now, run the code and see what we get in the message box. We got the result as 8 because VBA has certain codes for each kind of variable data type, so below is the detailed list for you.


1 Answers

From documentation

In VBScript, variables are always of one fundamental data type, Variant.

Data contained in the variable can be of any type, but variables itself are always of Variant type. Checking with VarType the contents of a position of an array will return the type of data contained. But the array itself is a compound of "cells" of Variant type

So, in your case VarType will return vbArray (8192) + vbVariant (12) = 8204

like image 93
MC ND Avatar answered Oct 05 '22 09:10

MC ND