-4

How would I go about programmatic making this list? I need all the combinations

string[] list = { "Open", "Completed","Rescheduled", "Canceled", "Started",
    "Customer notified", "Do Not Move", "Needs Confirmation" };

This list is the first 15 There is over 200+ combinations.

  1. Open
  2. Completed
  3. Open, Completed
  4. Rescheduled
  5. Open, Rescheduled
  6. Completed, Rescheduled
  7. Open, Completed, Rescheduled
  8. Canceled
  9. Open, Canceled
  10. Completed Canceled
  11. Open, Completed, Canceled
  12. Rescheduled, Canceled
  13. Open, Rescheduled, Canceled
  14. Completed, Rescheduled, Canceled
  15. Open, Completed, Rescheduled, Canceled
Odis Harkins
  • 291
  • 2
  • 13
  • How did you generate this list of 15 options? For example, #2 is `Completed` yet `"Completed"` is not in your `string[] list`... Your list of 15 combinations is not "all the combinations", as you ask for. – Jashaszun Jul 14 '15 at 16:22
  • Sorry I missed one of the strings in an array. – Odis Harkins Jul 14 '15 at 16:24
  • 2
    possible duplicate of [How can I obtain all the possible combination of a subset?](http://stackoverflow.com/questions/13765699/how-can-i-obtain-all-the-possible-combination-of-a-subset) – poke Jul 14 '15 at 16:26

2 Answers2

1

Don't use the lists for it. Try to use enum and attribute Flags like:

[Flags]
public enum Status
{
    Open = 0x01,
    Completed = 0x02,
    Rescheduled = 0x04,
    Canceled = 0x08,
    Started = 0x10,
    Customer_Notified = 0x20,
    Do_Not_Move = 0x40,
    Needs_Confirmation = 0x80
}

Then you can set a few statuses at once to field like

var status = Status.Open | Status.Completed
rbk
  • 111
  • 5
  • 1
    Are you sure? List statuses = new List(); for (int i = 1; i < 16; i++) { statuses.Add((Status)i); } and you have all statuses. Try in debug mode. – rbk Jul 14 '15 at 17:33
  • I need to be able to put this in a drop down list and send the number 1-230ish to the db so my app can allow access to the parts based on the number. – Odis Harkins Jul 14 '15 at 18:00
  • `List statuses = new List(); for (int i = 1; i < 16; i++) { statuses.Add((Status)i); } foreach (var status in statuses) { string test = status.ToString(); }` or you can use Description atribute to get another string – rbk Jul 14 '15 at 18:08
  • Your flag values will be wrong once you go past four values as 0x16 is not equal to 16 (decimal)! Remove the `0x` before each value. – Idle_Mind Jul 14 '15 at 19:41
  • ...or at least extend it out a couple more items so people can see the pattern of values: `1, 2, 4, 8, 16, 32, 64, 128` versus `0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80` – Idle_Mind Jul 14 '15 at 20:07
1

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.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40