中一个语句中的多个变量赋值

中一个语句中的多个变量赋值

本文介绍了Visual Basic 6 中一个语句中的多个变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用遗留代码时,我遇到了一些奇怪的变量赋值,我不确定它们是否是合法的 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.

  • ComStartComEndCR 是变体,而不是布尔值.
  • 在 VB6 中 = 是相等运算符,而不是 C 中的赋值运算符.
    • CR = False 不会改变 CR 的值.它将CR 的当前值与False 进行比较,如果CR 等于,则评估为True错误.假设它评估为 False
    • 现在您有了表达式 ComEnd = False.同样,这不会改变 ComEnd 的值.它将它与 False 进行比较,如果 ComEnd 等于 False,则评估为 True.这次假设它的计算结果为 True.
    • 现在您有了赋值语句 ComStart = True.这会将 ComStart 的值设置为 True
    • ComStart and ComEnd and CR are variants, not Booleans.
    • In VB6 = is the equality operator, not the assignment operator found in C.
      • CR = False does not change the value of CR. It compares the current value of CR to False, and evaluates as True if CR is equal to False. Let's say it evaluates as False
      • Now you have the expression ComEnd = False. Again, this does not change the value of ComEnd. It compares it with False, and evaluates as True if ComEnd is equal to False. This time let's say it evaluates as True.
      • Now you have the assignment statement ComStart = True. This sets the value of ComStart to True

      所以你的原始代码

      Dim ComStart, ComEnd, CR As Boolean
      ComStart = ComEnd = CR = False
      

      创建两个变量 ComStartComEnd 和一个布尔值 CR,然后

      Creates two variants ComStart and ComEnd and a Boolean CR, and then

      • CR 保持默认值,False
      • ComEnd 保持其 默认值, Empty
      • ComStart 设置为 False 因为 Empty = (Empty = False)False.
      • CR keeps its default value, False
      • ComEnd keeps its default value, Empty
      • ComStart is set to False because Empty = (Empty = False) is False.

      简单!...我希望遗留代码的其余部分更少,好吧,偶然.

      Simple! ... I hope the rest of the legacy code is less, well, accidental.

      这篇关于Visual Basic 6 中一个语句中的多个变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:04