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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With