Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  eNumerous Options Using Enumerations  »  Going Back To The Basics
eNumerous Options Using Enumerations
by Fadzai Chamba | Published  01/09/2006 | .NET Framework | Rating:
Fadzai Chamba
Fadz has been coding since 1993 when he was 13 years old when he first saw a computer that worked at St. Ignatius College in Harare Zimbabwe. His first programming experience was in GWBASIC. He thought he was learning the basics of computers and before long he was the renouned 'computer expert'. He later learned to use COBOL, Pascal, C, C++, PROLOG, Visual Basic, Java and Visual C++. Because of his first experiences he chose to ply his trade in Visual Basic. He came across .NET in July/August of 2003 and has been using it since. He has worked with VB(6) since October 2000, first as a Free lance developer, then for a company back to free lance. This yo-yo experience has continued to this day when he is now a Free Lance developer and starting a business in the Multi media field. He is a vbCity Guru who goes by the alias 'fabulous' and Spends a lot of his time inside .NET and the VB.NET and C# compilers. Fadz is also a DevCity .NETFella Award winner and constantly studies to improve his skills in .NET. 

View all articles by Fadzai Chamba...
Going Back To The Basics

Going back to the basics

The way we are reading the value of our enumeration is using a bitwise operation. Each value of the enumeration has a specific set of bits set, that is, a specific set of bits with the value of 1. For us to represent multiple values, we will need to do a little tap dancing around the bits so that we set alternating sets of bits for each option. This way, each option when set on its own will have one specific bit turned on.

This is the reason I numbered the values as powers of 2. For the purpose of simplifying our example, we will represent each value using 4 bits as follows:

<FLAGS()>Public Enum Stuff
    Item1 = 1       '0001
    Item2 = 2       '0010
    Item3 = 4       '0100
    Item4 = 8       '1000
End Enum

As you can see, setting both Item1 and Item3 will set two different bits to one. Using powers of 2 avoids having clashing values. Setting the first 3 items will have a value of 7 and this value can only be achieved by setting these three values. 7 in binary, as we all know, is represented this way:

0111

How do I read the values?

As I mentioned earlier, we read the values using a bitwise operator. In this case, the operator is AND.

To find out if the Item1 is selected, we would write code like this:

If CBool(s And Stuff.Item1) Then Console.WriteLine("Item1 is selected…")

It might be worth mentioning that the code can run without the CBool()  if you don't use Option Strict as I do. The code would, in that case, look like this:

If s And Stuff.Item1 Then Console.WriteLine("Item1 is selected…")

In any case, if the Item1 value had been selected, the corresponding message would be displayed to the console. I would recommend using CBool so that your code works whether or not someone is using Option Strict.

The DisplayStuff method uses a similar technique; here is all the code in it.

Console.WriteLine("Item1 set : {0}", CBool(s And Stuff.Item1))
Console.WriteLine("Item2 set : {0}", CBool(s And Stuff.Item2))
Console.WriteLine("Item3 set : {0}", CBool(s And Stuff.Item3))
Console.WriteLine("Item4 set : {0}", CBool(s And Stuff.Item4))

By dissecting the Item3 line, we can observe what's going on behind the scenes. As mentioned in the previous section, these values are set by turning some bits on, and some bits off.

     • When Item1 and Item3 are set, the value of s is equivalent to 0101 in binary.
     •   By using the operation (s And Item3), we are using the AND bitwise operation on the individual bits of the s variable, comparing it to the value of Item3 which is 0100.
    o This would result in the 0100. All the bits that don't correspond to a 1 in Item3's value are turned off, the 1 in this result is that way because the corresponding bit is 1.
   o This returns True because by using logic, anything that is not false is True. Since zero is false, anything that is not zero can be interpreted as True by the computer .

How do I turn a value off?

Supposing Item1 and Item3 are selected and the user then decided to turn Item1 off, the code needed to turn Item1 off without affecting anything else would use an AND operation rather than the usual OR. You would need to AND the current value with the complement of the item being deselected. What on earth is a complement? It is the result of a NOT operation, to put it quite simply. The code to turn Item1 off follows:

Dim s As Stuff = Stuff.All
    s = s And Not Stuff.Item1        'attempt to turn Item1 off

By doing that, the code is multiplying/ANDing the individual bits of the current value with the corresponding bits of the complement of Item1. The bit that is set to 1 in Item1 will now be zero and everything else is 1. This results in Item1's bit being turned off regardless of the previous value. Since everything else is ANDed by 1, everything else remains unchanged. As we all learned when starting mathematics, 1 multiplied by any value is whatever value we were multiplying with 1.

How does this work in real life?

Real life usage is not much different from what I presented here. In my current application I use this to keep track of user permissions. One variable manages 16 different restricted permission sets. The User class I have in my application has as many read-only properties and a method that is used to turn these permissions on or off. I trust that it won't be difficult to use these ideas in a real life project.

Conclusion

In this article, I discussed the role of enumerations marked as Flags for handling multiple options that can co-exist. This can be a useful technique and simplifies the management of multiple values. All these different values can be represented in a single variable and expansion (addition of new values) is very simple. By using this, you can come up with a large number of unnamed values by combining the different possible values. You will need to get familiar with the OR and AND bitwise operators. Feel free to comment or to contact me for further information.


Notes

1.    CBool is a function/operator in Visual Basic that converts an expression to Boolean. In C# or other languages, you can use Convert.ToBoolean() for the same purpose.

2.    If you download the project, you will see that I have used a conditional compilation variable to select between 2 sets of code, one that uses CBool, and one that doesn't. The values being represented as False are zero, and those being represented as True are equivalent to the value they are testing for, e.g. Item1 when testing for Item1. The code includes instructions on how to select the different versions.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.73333333333334 out of 5
 15 people have rated this page
Article Score22279
Sponsored Links