【问题标题】:Build a list of options based on an array基于数组构建选项列表
【发布时间】:2015-07-14 16:21:07
【问题描述】:

我将如何以程序化方式制作此列表?我需要所有的组合

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

此列表是前 15 种,有 200 多种组合。

  1. 打开
  2. 已完成
  3. 打开,已完成
  4. 改期
  5. 开放,重新安排
  6. 已完成,已重新安排
  7. 打开、完成、重新安排
  8. 已取消
  9. 打开,取消
  10. 已完成已取消
  11. 打开、完成、取消
  12. 已改期,已取消
  13. 打开、重新安排、取消
  14. 已完成、已重新安排、已取消
  15. 打开、完成、重新安排、取消

【问题讨论】:

  • 您是如何生成这个包含 15 个选项的列表的?例如,#2 是 Completed"Completed" 不在您的 string[] list 中...您的 15 种组合列表并不是您要求的“所有组合”。
  • 对不起,我错过了数组中的一个字符串。

标签: c# arrays list


【解决方案1】:

不要使用列表。尝试使用枚举和属性标志,如:

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

然后你可以一次设置几个状态到字段中

var status = Status.Open | Status.Completed

【讨论】:

  • 你确定吗?列表 状态 = 新列表(); for (int i = 1; i
  • 我需要能够将它放在下拉列表中并将数字 1-230ish 发送到数据库,以便我的应用程序可以允许根据数字访问部件。
  • List<Status> statuses = new List<Status>(); for (int i = 1; i < 16; i++) { statuses.Add((Status)i); } foreach (var status in statuses) { string test = status.ToString(); } 或者你可以使用描述属性来获取另一个字符串
  • 一旦超过四个值,您的标志值就会出错,因为 0x16 不等于 16(十进制)!删除每个值之前的0x
  • ...或至少将其扩展出更多项目,以便人们可以看到值的模式:1, 2, 4, 8, 16, 32, 64, 1280x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80
【解决方案2】:

这是一种使用字符串将其可视化的方法。数组中的每个点都可以被认为是二进制数中的一个位(0 或 1)。如果所有位都打开,这将为您提供最大组合数。因此,您从 1 迭代到最大数字,并包含数组中以该数字的二进制形式打开的那些值:

    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;
    }

* 编辑 *

这是同样的事情,但使用 rbks 方法的 enum 标记有 Flags 属性。这是我在上面所做的,但没有字符串操作;它只是在引擎盖下使用直接的数学和位操作。请注意,每个状态的值都是 2 的幂,并且没有在它们前面有0x。另请注意,值中不能有空格,因此我使用下划线,然后在字符串版本输出中替换它们:

    [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;
    }

这里有一个article 位标志,您可能会觉得有帮助。

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多