Here's a way to visualize it using strings. Each spot in the array can be thought of as a bit (0 or 1) in a binary number. If all bits are on, this gives you the max number of combinations. So you iterate from 1 to max number and include those values from the array that are toggled on in the binary form of that number:
private void button1_Click(object sender, EventArgs e)
{
string[] list = { "Open", "Completed","Rescheduled",
"Canceled", "Started", "Customer notified",
"Do Not Move", "Needs Confirmation" };
string binary;
int max = (int)Math.Pow(2, list.Length);
List<string> combo = new List<string>();
List<string> combinations = new List<string>();
for(int i = 1; i < max; i++)
{
binary = Convert.ToString(i, 2); ' convert it to a binary number as a string
char[] bits = binary.ToCharArray();
Array.Reverse(bits);
binary = new string(bits);
combo.Clear();
for(int x = 0; x < binary.Length; x++)
{
if (binary[x] == '1')
{
combo.Add(list[x]);
}
}
combinations.Add(String.Join(", ", combo));
}
// ... do something with "combinations" ...
listBox1.DataSource = null;
listBox1.DataSource = combinations;
}
* Edit *
Here's the same thing, but using rbks approach of an enum
marked with the Flags
attribute. This is doing what I did above, but without the string manipulation; it's just using straight math and bit manipulation under the hood. Note that the values of each state are the powers of two, and do not have 0x
in front of them. Also note that you can't have spaces in the values, so I used underscores and then replaced them in the string version output:
[Flags]
public enum Status
{
Open = 1,
Completed = 2,
Rescheduled = 4,
Canceled = 8,
Started = 16,
Customer_Notified = 32,
Do_Not_Move = 64,
Needs_Confirmation = 128
}
private void button2_Click(object sender, EventArgs e)
{
List<string> combinations = new List<string>();
Status status;
int max = (int)Math.Pow(2, Enum.GetValues(typeof(Status)).Length);
for(int i = 1; i < max; i++)
{
status = (Status)i;
combinations.Add(status.ToString().Replace("_", " "));
}
listBox1.DataSource = null;
listBox1.DataSource = combinations;
}
Here's an article on bit flags you might find helpful.