问题
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
回答1:
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"
.
回答2:
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)
来源:https://stackoverflow.com/questions/10218803/vb6-variant-type