Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flags in VB6 does not return a correct value

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.

like image 363
Thibault Falise Avatar asked Jan 07 '11 16:01

Thibault Falise


People also ask

What does the flags attribute do?

Enum Flags Attribute The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators.

What is Flag in VB net?

In general, "Flag" is just another term for a true/false condition. It may have more specific meanings in more specific contexts. For instance, a CPU may keep "arithmetic flags", each one indicating a true/false condition resulting from the previous arithmetic operation.

What are the first 4 numeric values of an enum used to store flags?

We use the values 0, 1, 2, 4 to indicate the underlying bits for each value—we should double each value to avoid conflicts. Operators We use bitwise operators, like OR and AND, with enum flags.


1 Answers

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)
like image 150
Flipster Avatar answered Oct 19 '22 01:10

Flipster