VB6 中的标志不返回正确的值

发布于 2024-10-11 01:50:51 字数 1181 浏览 1 评论 0原文

我目前正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-10-18 01:50:52

要测试标志的值,请使用类似以下内容:

lblAppleAvailable.Visible = (basket.AvailableFruits And Fruits.Apple) = Fruits.Apple

执行“AND”后,您仍然需要查看结果值是否等于标志值(或实际上不是 0 的任何值)。

您还可以创建一个小辅助函数:

Private Function HasFruitFlag(Check As Fruits, Flag As Fruits) As Boolean
    HasFruitFlag (Check And Flag) = Flag
End Function

您可以这样调用它:

lblAppleAvailable.Visible = HasFruitFlag(basket.AvailableFruits, Fruits.Apple)

To test the value of the flag, use something like this:

lblAppleAvailable.Visible = (basket.AvailableFruits And Fruits.Apple) = Fruits.Apple

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:

Private Function HasFruitFlag(Check As Fruits, Flag As Fruits) As Boolean
    HasFruitFlag (Check And Flag) = Flag
End Function

And you could call it like this:

lblAppleAvailable.Visible = HasFruitFlag(basket.AvailableFruits, Fruits.Apple)
始于初秋 2024-10-18 01:50:52

尝试使用本地变量

    Dim LocalFruits As Fruits

    LocalFruits = basket.AvailableFruits
    Debug.Print (LocalFruits And Apple) <> 0
    Debug.Print (LocalFruits And Strawberry) <> 0
    Debug.Print (LocalFruits And Lemon) <> 0

此外,您可能希望使用像这样的不易出错的枚举声明

Enum Fruits
    Apple = 2 ^ 0
    Strawberry = 2 ^ 1
    Lemon = 2 ^ 2
End Enum

Try using a local var

    Dim LocalFruits As Fruits

    LocalFruits = basket.AvailableFruits
    Debug.Print (LocalFruits And Apple) <> 0
    Debug.Print (LocalFruits And Strawberry) <> 0
    Debug.Print (LocalFruits And Lemon) <> 0

Also, you might want to use a less error-prone declaration of the enum like this

Enum Fruits
    Apple = 2 ^ 0
    Strawberry = 2 ^ 1
    Lemon = 2 ^ 2
End Enum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文