How large is a variant that is assigned a string value (1 Viewer)

sneuberg

AWF VIP
Local time
Today, 06:09
Joined
Oct 17, 2014
Messages
3,506
I was look at some code that I thought should produce a type mismatch but it didn't. A simulation of that code is:
Code:
Private Sub WhatsUp()

Dim X As Variant
Dim Y As Double

X = "A"
Debug.Print VarType(X)
Y = 1.79769313486231E+308

If X > Y Then
    Debug.Print "X > Y"
Else
    Debug.Print "X <= Y "
End If


End Sub

This produces "X > Y" in the immediate window even though Y is at the maximum value for a double. This makes me wonder how VBA is evaluating this internally. Does anyone know what's going on here?

PS: Debug.Print VarType(X) produces 8 which is a string type.
 

MarkK

bit cruncher
Local time
Today, 06:09
Joined
Mar 17, 2004
Messages
8,179
My bet is that what occurs is called a widening conversion. Check out this code, which shows a widening conversion from integer to long in the second expression . . .
Code:
Private Sub widensToLong()
    Dim int1 As Integer
    Dim int2 As Integer
    Dim lng2 As Long
    
    Debug.Print TypeName(int1 * int2) [COLOR="Green"]'integer[/COLOR]
    Debug.Print TypeName(int1 * lng2) [COLOR="Green"]'expression result 'widens' to long[/COLOR]
End Sub
I believe your expression widens to the widest variable type Y, which is a Variant/String, and in that case...
"A" < "1.5xxxxx" = True
That's my guess.
 

sneuberg

AWF VIP
Local time
Today, 06:09
Joined
Oct 17, 2014
Messages
3,506
Thanks. After some additional testing I see that it's converting the double to a string. . For example if I change X to "0" I get "X <= Y " in the immediate window.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:09
Joined
Feb 28, 2001
Messages
27,140
Yep, MarkK called it before I got to it.

VBA, because it is based on a more or less "standard" version of BASIC, does automatic conversions behind the scenes and it has to find the lowest common denominator it can use for that comparison - which in this case appears to be a string.

Having X as a variant is part of the problem, in a way. That is an open invitation to convert the other comparand to something else.
 

MarkK

bit cruncher
Local time
Today, 06:09
Joined
Mar 17, 2004
Messages
8,179
comparand
Wow, I've never heard this word before, but it makes total sense.

Also, The_Doc_Man, congrats, by the way, on your recent retirement! :)
Wishing you all the best,
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:09
Joined
Feb 28, 2001
Messages
27,140
Thanks, Mark. As of 9/1/16, I'm a free man. I miss the people but don't miss the stress. Last week my blood pressure was down to 118/68. For me, that's real progress.

As to those words, the "Doc" in my name is quite real. I learned my math when folks still used those words. The two things being added are "addends" and the two things being multiplied are "multiplicands." We already use different names for the divisor and dividend, and those are still commonly used. Then there are "subtrahends" and "comparands." All perfectly good.
 

Users who are viewing this thread

Top Bottom