VB6 Variant type

放肆的年华 提交于 2020-01-13 20:30:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!