Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  eNumerous Options Using Enumerations  »  Selecting One Item
eNumerous Options Using Enumerations
by Fadzai Chamba | Published  01/09/2006 | .NET Framework | Rating:
Selecting One Item

Selecting One Item

   Selecting one item is no different from assigning a value the way you would do it in the normal way. Having declared a variable of type Stuff, all you need to do is assign the desired value. Here is a quick example:

Sub Main()
    Dim s As Stuff = Stuff.Item1

    DisplayStuff(s)
    Console.ReadLine()
End Sub

The following are the results you would get if you ran this code.

Item1 set : True
Item2 set : False
Item3 set : False
Item4 set : False

Selecting more than one item

   Having seen how to do it with one option selected, it follows that it is very simple to specify a second option. So if you wanted Item1 and Item3 together, this code looks logical:

Sub Main()
    Dim s As Stuff = Stuff.Item1
    s = Stuff.Item3

    DisplayStuff(s)
    Console.ReadLine()
End Sub

Running this code produces the following results:

Item1 set : False
Item2 set : False
Item3 set : True
Item4 set : False

What happened? It seems only Item3 is selected. To have more than one item selected, another strategy is needed. To solve this, go back to the section labeled Basics and read what it says under the Boolean algebra topic.


Bringing the basics forward

Having gone back to the basics, bring them back to where we are; that is, forward. When we want a second option enabled, what we are in essence doing is adding an option, or we are ORring an option. This is how we do it.

Sub Main()
    Dim s As Stuff = Stuff.Item1
    s = s Or Stuff.Item3

    DisplayStuff(s)
    Console.ReadLine()
End Sub

Running this code will show that both Item1 and Item3 are selected as expected. So, someone may be wondering why we had to use the OR operation.

Sponsored Links