Using the BitArray to reduce the nesting of conditional branches.

In Visual Basic, you might find yourself in a situation with so many conditions and possibilities to evaluate that you can't find a way around using nested conditional branches. While one possible way to overcome this would be with functions upon functions, but what if there were an easier way?
Well luckily there is -with a Bitarray! While I don't see the bitarray used as frequently as others might, I think it would be a good implementation.

Lets say for demonstration purposes you have 3 boolean conditions which we will call A, B, and C. For these 3 conditions, we already have a possibility of 8 branches, but what happens when each branch needs to be evaluated, or the other? Then we have to start nesting, whether it be with if-then statements or with select-case statements. This can quickly become confusing!

'Some settings
Dim a As Boolean  = True
Dim b As Boolean  = True
Dim c As Boolean  = True
 
'Imagine nesting this into conditional branches
 
'a=False b=False c=False 
'a=False b=False c=True  
'a=False b=True c=False  
'a=False b=True c=True   
'a=True b=False c=False  
'a=True b=False c=True   
'a=True b=True c=False   
'a=True b=True c=True

So a shortcut around the confusing part is first realizing that all possible combinations can correspond to a binary number:

'All possible combinations correspond to a binary number!
 
'a=False b=False c=False '00000000
'a=False b=False c=True  '00000001
'a=False b=True c=False  '00000010
'a=False b=True c=True   '00000011
'a=True b=False c=False  '00000100
'a=True b=False c=True   '00000101
'a=True b=True c=False   '00000110
'a=True b=True c=True    '00000111

The above truth table is a list of all the possible combinations for only 3 variables. 8 combinations! The possibilities are exponential(in a base 2 kind of way) for each setting you add. With 4 variables, there would be 16 possible combinations!

Keep in mind, for a lot of settings, it would be better to simply evaluate some settings on a per-setting basis, and take action individually. But some instances demand that the combination of settings determines specific effects of the program, this is where using bit arrays to reduce nesting can be helpful. You can also overcome some of these nesting issues with "AndAlso", although this can get ugly too!

So that being said, you would set the boolean values in an array of Boolean values at the relevant index. Once you have set all your values in the boolean array, you pass that array to the constructor of the BitArray class. Finally, you can convert that bitarray into an integer. With that integer, you can determine the selected combination of values.

Full Example:

01.Option Strict On
02.Option Explicit On
03.Option Infer Off
04.Public Class  Form1
05.    Private Sub  Form1_Load(sender As  Object, eA As EventArgs) Handles MyBase.Load
06.        'Some settings
07.        Dim a As Boolean  = True
08.        Dim b As Boolean  = True
09.        Dim c As Boolean  = True
10.        'There are 32 bits in an integer, so we create an array of bits(true/false)
11.        Dim bits(31) As Boolean
12.        'Assign each value to the array
13.        bits(0) = a
14.        bits(1) = b
15.        bits(2) = c
16.        'Create a new bitarray
17.        Dim bitArray As New  BitArray(bits)
18.        'Convert the bitarray into an integer
19.        Dim userSelection As Integer  = ToInteger(bitArray)
20.        'Determine a course of action based on which combination of
21.        'settings is chosen
22.        Select Case  userSelection
23.            Case 0 : MsgBox("a=False b=False c=False   00000000")
24.            Case 1 : MsgBox("a=False b=False c=True    00000001")
25.            Case 2 : MsgBox("a=False b=True  c=False   00000010")
26.            Case 3 : MsgBox("a=False b=True  c=True    00000011")
27.            Case 4 : MsgBox("a=True  b=False c=False   00000100")
28.            Case 5 : MsgBox("a=True  b=False c=True    00000101")
29.            Case 6 : MsgBox("a=True  b=True  c=False   00000110")
30.            Case 7 : MsgBox("a=True  b=True  c=True    00000111")
31.        End Select
32.    End Sub
33.    Private Function  ToInteger(bitArray As BitArray) As Integer
34.        'Create an array of integer with 1 integer
35.        '(because we are providing exactly 32 bits, and an integer is 32 bits long)
36.        Dim array(0) As Integer
37.        'Copy an integer to the array
38.        bitArray.CopyTo(array, 0)
39.        'Return the first integer in the array
40.        Return array(0)
41.    End Function
42.End Class

I hope you find this helpful, Please update this article if I left anything out!
Please view my other TechNet Wiki Articles


Wordpress Counter