VB6 变体类型 [英] VB6 Variant type
问题描述
我来自面向对象的背景.为什么在此代码片段中显示(在消息框中)测试"(注意引号).我希望逻辑测试: 'If Test = "True"' 返回 False,因为该变体包含一个布尔值而不是一个字符串.因此,我不希望出现消息框,但确实如此:
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
推荐答案
Variant
VB6(以及大多数支持它们的其他语言)中的类型值会根据需要在数据类型之间自动转换;它们在 COM 交互中被广泛使用.
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
不过,在这里,您根本没有使用变体;您使用的是硬编码字符串 "Test"
.
Here, though, you're not using the variant at all; you're using a hard-coded string "Test"
.
这篇关于VB6 变体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!