VB6 中的标志不返回正确的值
我目前正在尝试在 VB6 / COM 项目中使用位标志枚举。 但是,当尝试从枚举读取值时,我得到不一致的结果。
这是枚举定义:
Enum Fruits
None = 0
Apple = 1
Strawberry = 2
Lemon = 4
End Enum
我有一个对象,它公开了 Fruits 类型的属性。
Public Get AvailableFruits as Fruits
应该能够读取该值的代码用于根据枚举的每个位的值显示/隐藏标签:
lblAppleAvailable.Visible = basket.AvailableFruits And Fruits.Apple
当此代码为执行后,我有 basket.AvailableFruits = 0
,结果为 True。
知道什么可能导致这种行为吗?
编辑:
我尝试过使用枚举的不同值:
basket.AvailableFruits = 0
basket.AvailableFruits And Apple // Returns True
basket.AvailableFruits And Strawberry // Returns True
basket.AvailableFruits And Lemon // Returns False
作为辅助节点,在调试代码时,如果我将表达式放入监视表达式中,我会得到正确的值;但是当在我的代码中计算表达式时,它仍然返回 True。
我尝试使用不同的检查语法:
(basket.AvailableFruits And Fruits.Apple) = Fruits.Apple
在 basket.AvailableFruits = 0
时仍然为 True :-(
解决方案
在测试了不同的解决方案之后,我已经能够将问题缩小到COM 组件。 该组件的原始编码器将指针设置为 0,而不是返回 0 作为值,这导致在尝试读取该值时出现问题。
我选择了 FlipScript 答案,因为辅助函数似乎是提高代码可读性的一个很好的技巧。
I am currently trying to use a bit flag enum in a VB6 / COM project.
However, when trying to read values from the enum, I get inconsistent results.
Here is the enum definition :
Enum Fruits
None = 0
Apple = 1
Strawberry = 2
Lemon = 4
End Enum
I have an object which exposes a property of type Fruits
Public Get AvailableFruits as Fruits
The code that should be able to read the value is used to show / hide a label depending on the values of each bit of the enum :
lblAppleAvailable.Visible = basket.AvailableFruits And Fruits.Apple
When this code is executed and I have basket.AvailableFruits = 0
, I get True as result.
Any idea of what could cause this behavior ?
Edit :
I have tried with the different values of the enum :
basket.AvailableFruits = 0
basket.AvailableFruits And Apple // Returns True
basket.AvailableFruits And Strawberry // Returns True
basket.AvailableFruits And Lemon // Returns False
As a side node, when debugging the code, If I put the expression in a watch expression, I get the correct value; but when the expression is evaluated in my code, it still returns True.
I tried using a different check syntax :
(basket.AvailableFruits And Fruits.Apple) = Fruits.Apple
Still getting True when basket.AvailableFruits = 0
:-(
Solution
After having tested different solutions, I have been able to narrow the problem to the COM component.
The original coder of this component had a pointer set to 0 instead of returning 0 as a value, which caused the problem when trying to read the value.
I have selected FlipScript answer because of the helper function which seems a good tip to improve the readability of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要测试标志的值,请使用类似以下内容:
执行“AND”后,您仍然需要查看结果值是否等于标志值(或实际上不是 0 的任何值)。
您还可以创建一个小辅助函数:
您可以这样调用它:
To test the value of the flag, use something like this:
After you do the "AND", you still need to see if the resulting value equals the flag value (or anything other than 0, really).
You could also create a little helper function:
And you could call it like this:
尝试使用本地变量
此外,您可能希望使用像这样的不易出错的枚举声明
Try using a local var
Also, you might want to use a less error-prone declaration of the enum like this