Let me preface this by saying that I've only taken 1 formal programming class and everything else I've learned is from trial and error or Google (forums, blogs). I have never taken an algorithm class or an advanced math class.

In the process of making my current application, I need to perform multiple checks and need complex if then statements in order to eliminate multiple possibilities (my datagridview, which I'm validating has at minimum of 9 fields to validate) all of which will require a separate function call.

In the process of doing this, I've noticed that I've begun to miss combinations and am looking to write a quick application to tell me the what all the combinations are. Essentially it'll have a listbox for the placeholders I'll be using to replace with the boolean operation in the project. The listbox will be associated with 1 boolean value in a boolean array. As it's looping through the array showing all the possibilities, I'll be lining up the values with the listbox.

The problem I'm having relates to how to output all the possible combinations of True/False values any number of booleans. How would I process through the array and flip the values as they need to be?

The output, for 3 values, I'm looking for would consist of:
F:ListBoxValue1 F:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 F:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 T:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 T:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 T:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 F:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 T:ListBoxValue2 F:ListBoxValue3

I don't need the code for the form or even include the listbox, just how to get all the combinations of the boolean array.

Thanks in advance!

with 9 boolean variables that is 512 combinations

If you are not into maths this is going to freak you out a bit but the best approach is to resort to binary. An integer in binary form consists of a series of 1s and 0s that can represent your 9 variables

eg 010101011 = 171


the left most 0 represents your first listbox
second digit (1) represents listbox 2
third digit (0) represents listbox 3
and so on.

this is achieved in .NET with a BitArray. In the example below I have set values 1 & 3 arbitarily. In your code you will set all nine elements of the bit array based on your list boxes.

the function BinToInt converts the bit array to an integer between 0 and 511 which represents to 512 different combinations.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim LineIn As String
        LineIn = ""
        Dim Int1 As Int32
        Dim test As New BitArray(9, False)
        
        
        test.Item(1) = True
        test.Item(3) = True
        Int1 = BinToInt(test, 9)
        Console.WriteLine(Int1.ToString)
        

    End Sub
    Public Function BinToInt(ByVal BAin As BitArray, ByVal BAdigits As Int16) As Int16
        Dim i As Int16
        Dim outint As Int16
        outint = 0
        For i = 0 To BAdigits - 1
            If BAin(i) Then
                outint = outint + 2 ^ i
            End If
        Next i
        BinToInt = outint
            
    End Function

with 9 boolean variables that is 512 combinations

Thanks for the help!

There are restrictions on most of the combinations. For example, the last two can only be used when the first one is true. And even then both the last two can be independent or together with each other.

In your code you will set all nine elements of the bit array based on your list boxes.

I apologize for confusing the problem, the listbox is used as place holder for me to interpret into the code, not to set the values. I.e.: That way "TFFFFTFTT" would turn into "T: Partnumber F:OrderNumber F:LineNumber",etc.

The problem I'm having is determining all the outcomes for the amount of items listed in the list box. If there are 3 items, then there'll be 8 combinations (man I hope my math is right, not enough coffee yet), 9 then 512 combinations. I need the code to take in the n boolean options and loop through and tell me what all the combinations are.

I presume these are coming from user input, in which case it may be simpler to restrict your inputs to valid combinations than try and interpret them later.

For example if we stick to 4 inputs for simplicity. If 3 & 4 can only be supplied if 1 is supplied then write some client side script to run on exit from 1. if 1 is Null then disable 3 and 4, otherwise enable 3 & 4. This way you only need to cater for valid combinations in your code.

I finally took the time to attempt to find a solution. From what I can tell it's working properly, but if you see any errors, please let me know.

I figured attaching my finished product would help anyone else who was looking for something similar.

Note: the file uploaded is a Visual Basic file and the extension needs to be changed to "vb" to run it. Compiled in VB .Net 2.0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.