Discussion:
Variable type questions
(too old to reply)
JJ
2018-04-26 19:16:12 UTC
Permalink
In MSDN's "VarType()" document, it states:

"For example, the value returned for an array of integers is calculated as 2
+ 8192, or 8194."

However, the value is 8204 instead of 8194, when I test it using below code.

dim a(0)
a(0) = 1
wscript.echo vartype(a) 'shows 8204

8204 in hexadecimal is 0x200C, which is a combination of vbArray and
vbVariant. Meaning that it's an array of variants, instead of an array of
integer. So, which one is correct?

To add into the confusion, the contents of the array doesn't really matter.
i.e.

dim a(0)
a(0) = true
wscript.echo vartype(a) 'still shows 8204
a(0) = "abc"
wscript.echo vartype(a) 'still shows 8204
dim b(0)
a(0) = b
wscript.echo vartype(a) 'still shows 8204

Also, for research purpose, there are two types that I'm unable to produce:
vbError and vbDataObject. For vbError, I immediately thought of the Error
object. But its type is actually vbObject. So, what class/component produces
such types?
Mayayana
2018-04-26 21:17:42 UTC
Permalink
"JJ" <***@vfemail.net> wrote

| 8204 in hexadecimal is 0x200C, which is a combination of vbArray and
| vbVariant. Meaning that it's an array of variants, instead of an array of
| integer. So, which one is correct?
|

The whole thing seems kind of hokey to me.
An array is a variant and it's an array of variants,
so returning 8204 makes sense to me. But returning
2 for a number or 8 for "apple" is misleading, since
that's only the current subtype and the functional
type is still variant.
JJ
2018-04-27 19:44:23 UTC
Permalink
Post by Mayayana
| 8204 in hexadecimal is 0x200C, which is a combination of vbArray and
| vbVariant. Meaning that it's an array of variants, instead of an array of
| integer. So, which one is correct?
|
The whole thing seems kind of hokey to me.
An array is a variant and it's an array of variants,
so returning 8204 makes sense to me. But returning
2 for a number or 8 for "apple" is misleading, since
that's only the current subtype and the functional
type is still variant.
All VB/VBScript variables are indeed variant type. VarType() returns the
variant subtype for the variable. It doesn't return the actual type of the
variable (which would always be vbVariant). However, the result conflicts
with the documentation when it comes to array.

The TypeName() function result is also the same as VarType() when it comes
to array. It returns "Variant()". With the "()" suffix, denoting an array.
R.Wieser
2018-04-27 07:38:05 UTC
Permalink
JJ,
Post by JJ
8204 in hexadecimal is 0x200C, which is a combination of vbArray
and vbVariant. Meaning that it's an array of variants, instead of an
array of integer. So, which one is correct?
The first one.

Question: If you fill a(0) with 1234 and a(1) with "world", what type would
the array "a" than need to be and return ? (for fun, imagine some more
data types in that array: objects, booleans, floats, dates, etc. :-) )

No, the *array* is of type variant, which each *element* being of type
variant too. Each of those variant elements can than ofcourse contain any
type of data you please (the strong point of using variants - and ofcourse
its weak point too).

By the way, its posible to actually create a variant array of ints, strings
or whatever, just not in VBScript* - as it only knows variants.

*It can manage/move them around just fine (accepting them from and providing
them to a function), it just cannot access them.
Post by JJ
To add into the confusion, the contents of the array doesn't really matter.
i.e.
wscript.echo vartype(a) 'still shows 8204
Have you also tried "vartype( a(0) )" ? What did you get ?

In other words, this is what it looks like in VBScript:

variant array (1) -> variant element (2) -> contents (3)

while you are (probably) *thinking* of this :

variant array -> contents

You are asking what the array (at #1) is, and what its contents are. That
means you are getting a result which is a combination of it (1) and its
"child" (2). Nothing more, nothing less.

If you want to know something about the type of the contents (at 3), you
have to inquire its "parent", the variant element (at 2).

And by the way, a "variant element" (as I named it in the above) is the very
same thing as a "variant variable" (or "variable" for short).

Hope that helps.
Post by JJ
Also, for research purpose, there are two types that I'm unable to
produce: vbError and vbDataObject.
Can't help you with that I'm afraid.

Regards,
Rudy Wieser
JJ
2018-04-27 19:56:45 UTC
Permalink
Post by R.Wieser
JJ,
Post by JJ
8204 in hexadecimal is 0x200C, which is a combination of vbArray
and vbVariant. Meaning that it's an array of variants, instead of an
array of integer. So, which one is correct?
The first one.
My hunch tells me that too. Considering that it's pretty common for MSDN to
have incorrect information.
Post by R.Wieser
Question: If you fill a(0) with 1234 and a(1) with "world", what type would
the array "a" than need to be and return ? (for fun, imagine some more
data types in that array: objects, booleans, floats, dates, etc. :-) )
No, the *array* is of type variant, which each *element* being of type
variant too. Each of those variant elements can than ofcourse contain any
type of data you please (the strong point of using variants - and ofcourse
its weak point too).
By the way, its posible to actually create a variant array of ints, strings
or whatever, just not in VBScript* - as it only knows variants.
*It can manage/move them around just fine (accepting them from and providing
them to a function), it just cannot access them.
Couldn't agree more.
Post by R.Wieser
Post by JJ
Also, for research purpose, there are two types that I'm unable to
produce: vbError and vbDataObject.
Can't help you with that I'm afraid.
Aw, bummer.
Roger Roesler
2018-04-29 09:35:26 UTC
Permalink
Post by JJ
"For example, the value returned for an array of integers is
calculated as 2 + 8192, or 8194."
However, the value is 8204 instead of 8194, when I test it using below code.
dim a(0)
a(0) = 1
wscript.echo vartype(a) 'shows 8204
8204 in hexadecimal is 0x200C, which is a combination of vbArray and
vbVariant. Meaning that it's an array of variants, instead of an
array of integer. So, which one is correct?
To add into the confusion, the contents of the array doesn't really
matter. i.e.
dim a(0)
a(0) = true
wscript.echo vartype(a) 'still shows 8204
a(0) = "abc"
wscript.echo vartype(a) 'still shows 8204
dim b(0)
a(0) = b
wscript.echo vartype(a) 'still shows 8204
Also, for research purpose, there are two types that I'm unable to
produce: vbError and vbDataObject. For vbError, I immediately thought
of the Error object. But its type is actually vbObject. So, what
class/component produces such types?
This script performs logical conjunctions on two expressions:

'######################################################################
Dim a(0), out

a(0) = 1
WScript.Echo "VarType(a) = " & VarType(a)
WScript.Echo "TypeName(a(0)) = " & TypeName(a(0))
WScript.Echo "VarType(a) And vbArray = " & _
CInt(VarType(a) And vbArray)
WScript.Echo "VarType(a) Xor vbVariant = " & _
CInt(VarType(a) Xor vbVariant)
Set out = WScript.StdOut
out.WriteLine "VarType(out) And vbObject: " & _
CBool(VarType(out) And vbObject)
out.WriteLine "VarType(out) : " & VarType(out)

On Error Resume Next ' raise an error
out.WriteLine "VarType(a) And vbArray = " & VarType(a) And vbArray
If Err Then
Set out = WScript.StdErr
out.WriteLine vbCRLF & "VarType(Err) : " & VarType(Err)
out.WriteLine "TypeName(Err) : " & TypeName(Err)
out.WriteLine "VarType(Err) And vbError : " & _
CBool(VarType(Err) And vbError)
out.WriteLine "VarType(Err) And vbObject: " & _
CBool(VarType(Err) And vbObject)
out.WriteLine "Error " & Err.Number & ": " & Err.Description
Err.Clear
End If
'######################################################################

The result is somewhat unexpected. HTH
--
ЯR
JJ
2018-04-29 15:51:25 UTC
Permalink
Post by Roger Roesler
'######################################################################
Dim a(0), out
a(0) = 1
WScript.Echo "VarType(a) = " & VarType(a)
WScript.Echo "TypeName(a(0)) = " & TypeName(a(0))
WScript.Echo "VarType(a) And vbArray = " & _
CInt(VarType(a) And vbArray)
WScript.Echo "VarType(a) Xor vbVariant = " & _
CInt(VarType(a) Xor vbVariant)
Set out = WScript.StdOut
out.WriteLine "VarType(out) And vbObject: " & _
CBool(VarType(out) And vbObject)
out.WriteLine "VarType(out) : " & VarType(out)
On Error Resume Next ' raise an error
out.WriteLine "VarType(a) And vbArray = " & VarType(a) And vbArray
If Err Then
Set out = WScript.StdErr
out.WriteLine vbCRLF & "VarType(Err) : " & VarType(Err)
out.WriteLine "TypeName(Err) : " & TypeName(Err)
out.WriteLine "VarType(Err) And vbError : " & _
CBool(VarType(Err) And vbError)
out.WriteLine "VarType(Err) And vbObject: " & _
CBool(VarType(Err) And vbObject)
out.WriteLine "Error " & Err.Number & ": " & Err.Description
Err.Clear
End If
'######################################################################
The result is somewhat unexpected. HTH
You shouldn't apply bitwise operator on variable types because variable type
values mostly are not bit masks (except vbArray).

In that code, vbError is 10, and vbObject is 9. AND-ing them would result to
8, where if it's converted to boolean, it will be true. Because numbers
which isn't zero, will always evaluate to true if it's converted to boolean.

So, instead of:

CBool(VarType(Err) And vbError)

It should be:

((VarType(Err) And &HFFF) = vbError)
Roger Roesler
2018-04-29 17:54:16 UTC
Permalink
Post by JJ
Post by Roger Roesler
The result is somewhat unexpected. HTH
You shouldn't apply bitwise operator on variable types because
variable type values mostly are not bit masks (except vbArray).
In that code, vbError is 10, and vbObject is 9. AND-ing them would
result to 8, where if it's converted to boolean, it will be true.
Because numbers which isn't zero, will always evaluate to true if
it's converted to boolean.
CBool(VarType(Err) And vbError)
((VarType(Err) And &HFFF) = vbError)
Ok, thanks!
--
ЯR
Continue reading on narkive:
Loading...