3

I come from an Object Oriented background. Why does "Test" (notice the quotes) display (in the message box) in this code fragment. I would expect the logical test: 'If Test = "True"' to return False because the variant contains a Boolean and not a String. Therefore I would not expect the Message Box to appear, but it does:

Dim Test As Variant

Test = True

If Test = "True" Then //line 5
    MsgBox ("Test")
End If
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • 1
    Your code tries to compare logical yes/no to "foobar" as far as the compiler can tell. Which way do you like it to work? Should the compiler generate code to convert the string to a boolean and then compare? Or should it generate code to convert the boolean to a string? Your program won't crash when you replace "True" by "foobar". Try it. Now you know. – Hans Passant Apr 19 '12 at 00:20

2 Answers2

10

Variant type values in VB6 (and most other languages that support them) automatically convert between data types as needed; they're used extensively in COM interactions.

The code you're using uses the automatic (implicit) conversion from boolean to string here:

if Test = "True"

after using it as it's original assigned type (boolean) here

Test = True

Here, though, you're not using the variant at all; you're using a hard-coded string "Test".

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    I realise why the word Test is appearing in the MessageBox and not True/False! The second part of your answer addresses my concern. Is the variable: Test implicitly casted into a String on line 5 of my code (I have labelled line 5 in the original question)? – w0051977 Apr 18 '12 at 22:36
  • 1
    @KirkWoll, you're right. Removed irrelevant text. Thanks. :) w0051977: Yes, it's an implicit (auto) conversion as needed based on usage; that's why it's called a `variant` (the type changes as necessary). – Ken White Apr 18 '12 at 22:44
  • 3
    VB6 will automatically convert between data types for **any** data type, not just for variants as implied in this answer. This conversion is sometimes called implicit type conversion or evil type coercion ([pdf](http://vb.mvps.org/articles/pt199511.pdf)). It's widely disliked, which is why [Option Strict was introduced](http://stackoverflow.com/questions/222370) in VB.Net. – MarkJ Apr 19 '12 at 12:23
  • 1
    @MarkJ: I think that's what I said in the first sentence - "automatically convert between data types as needed". I also show it converting a boolean to a string. What part do you think is wrong (so I can re-read it to see if I can phrase it better)? – Ken White Apr 19 '12 at 12:34
  • 2
    I read the first sentence as implying that the type of the Test variable was important. But VB6 would behave the same if Test was declared as Boolean. Or if you compare literals. If True = "True" Then MsgBox "aha!" The fact that Test is Variant isn't relevant to type coercion. – MarkJ Apr 19 '12 at 16:49
1

They reason why the word Test is appearing in the MessageBox is because you are showing the string "Test" in your message box

MsgBox ("Test")

You should use this

MsgBox (Test)
Nick
  • 1,128
  • 7
  • 12
  • 2
    You [shouldn't use brackets around the parameters when not using the result of the function](http://stackoverflow.com/a/10108489/588306) or `Call`. – Deanna Apr 19 '12 at 08:22