问题描述
使用遗留代码时,我遇到了一些奇怪的变量赋值,我不确定它们是否是合法的 VB6 语法,但我找不到支持这种感觉的文档.
Working with legacy code I came across some strange variable assignments that I am not sure are legal VB6 syntax, but I cannot find the documentation to back up the feeling.
Dim ComStart, ComEnd, CR As Boolean
ComStart = ComEnd = CR = False
我的怀疑是
a) 原始声明应该是
Dim ComStart 为布尔值,ComEnd 为布尔值,CR 为布尔值
b) 现在实现的声明不会为 ComStart
分配任何内容.
b) the declarations as they are implemented now will not assign anything to ComStart
.
非常感谢任何答案或文档
Any answers or documentation are much appreciated
推荐答案
您找到的代码在技术上合法 VB6,因为它可以编译和运行.但是很可能原作者认为代码会做一些不同的事情!有两个误解.
The code you have found is technically legal VB6, because it compiles and runs. But it is very likely that the original author thought the code would do something different! There are two misunderstandings.
ComStart
和ComEnd
和CR
是变体,而不是布尔值.- 在 VB6 中
=
是相等运算符,而不是 C 中的赋值运算符.CR = False
不会改变CR
的值.它将CR
的当前值与False
进行比较,如果CR
等于,则评估为
.假设它评估为True
错误False
- 现在您有了表达式
ComEnd = False
.同样,这不会改变ComEnd
的值.它将它与False
进行比较,如果ComEnd
等于False
,则评估为True
.这次假设它的计算结果为True
. - 现在您有了赋值语句
ComStart = True
.这会将ComStart
的值设置为True
ComStart
andComEnd
andCR
are variants, not Booleans.- In VB6
=
is the equality operator, not the assignment operator found in C.CR = False
does not change the value ofCR
. It compares the current value ofCR
toFalse
, and evaluates asTrue
ifCR
is equal toFalse
. Let's say it evaluates asFalse
- Now you have the expression
ComEnd = False
. Again, this does not change the value ofComEnd
. It compares it withFalse
, and evaluates asTrue
ifComEnd
is equal toFalse
. This time let's say it evaluates asTrue
. - Now you have the assignment statement
ComStart = True
. This sets the value ofComStart
toTrue
所以你的原始代码
Dim ComStart, ComEnd, CR As Boolean ComStart = ComEnd = CR = False
创建两个变量
ComStart
和ComEnd
和一个布尔值CR
,然后Creates two variants
ComStart
andComEnd
and a BooleanCR
, and thenCR
保持默认值,False
ComEnd
保持其 默认值,Empty
ComStart
设置为False
因为Empty = (Empty = False)
是False
.
CR
keeps its default value,False
ComEnd
keeps its default value,Empty
ComStart
is set toFalse
becauseEmpty = (Empty = False)
isFalse
.
简单!...我希望遗留代码的其余部分更少,好吧,偶然.
Simple! ... I hope the rest of the legacy code is less, well, accidental.
这篇关于Visual Basic 6 中一个语句中的多个变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!