将值列表分配给可缩放的自定义对象列表

时间:2016-08-01 16:52:59

标签: c# loops variable-assignment

下面是我可以在其他地方生成的列表中找到的最大对象集的示例。每组中可能会有更少的组或更少的值。

CustomObject COOLING_111; //Start of Cooling group 1 - section 1
CustomObject COOLING_112;
CustomObject COOLING_113;
CustomObject COOLING_114;
CustomObject COOLING_115;
CustomObject COOLING_116;
CustomObject COOLING_117;
CustomObject COOLING_118;


CustomObject COOLING_121; //Start of Cooling group 1 - section 2
...
CustomObject COOLING_128


CustomObject COOLING_211; //Start of Cooling group 2 - section 1
...
CustomObject COOLING_218;


CustomObject COOLING_221; //Start of Cooling group 2 - section 2
...
CustomObject COOLING_228;


CustomObject COOLING_311; //Start of Cooling group 3 - section 1
...
CustomObject COOLING_318;


CustomObject COOLING_321; //Start of Cooling group 3 - section 2
...
CustomObject COOLING_328;


CustomObject COOLING_411; //Start of Cooling group 4 - section 1
...
CustomObject COOLING_418;


CustomObject COOLING_421; //Start of Cooling group 4 - section 2
...
CustomObject  COOLING_428;

如何编辑/创建循环或条件语句序列,以便按照示例所描述的顺序专门分配我的数组中的变量:

  • 将每个对象的值设置为-1。
  • 以下列模式设置任意数量对象的值(例如前6个):
    • 每组的第一个对象(第1部分)
    • 然后是每组的第一个对象(第2节)
    • 然后返回每组的第二个对象(第1部分)
    • 最后来自每个小组的第二个对象(第2节)
    • e.g。 111 - > 211 - > 311 - > 411 - > 121 - > 221 - > 321 - > 421 - > 112 - > ... - > 122 - >等

目前,无论CustomObjects冷却列表的大小如何,我都按照应该分配的顺序创建一个值数组。冷却列表中的对象是无序的,只能通过解析名称中的索引来区分。如果示例大小中的数组实际上是6,那么在根据上面的示例分配221后,您将停止。

int count = 0;
Boolean init1 = false;
Boolean init2 = false;
Boolean init3 = false;
Boolean init4 = false;
values = new int[6] {12, 18, 9, 56, 112, 187} //Simplified but normally some code is abstracted and this array comes from another part of my code

do{
  foreach (CustomObject obj in objList) {
    obj.value = -1;
    if(count < values.Length) {
      string name1 = obj.Name.substring(8);
      if (name1.StartWith("1")) {
        if (!init1) {
          obj.Value = values[count++];
          init1 = true;
        }
      }
      if (name1.StartsWith("2")) {
        if (!init2) {
          obj.Value = values[count++];
          init2 = true;
        }
      }
      if (name1.StartsWith("3")) {
        if (!init3) {
          obj.Value = values[count++];
          init3 = true;
        }
      }
      if (name1.StartsWith("4")) {
        if (!init4) {
          obj.Value = values[count++];
          init4 = true;
          break;
        }
      }
      if ((count % 4 == 0) && (count > 0) && (count < values.Length)) {
        init1 = false;
        init2 = false;
        init3 = false;
        init4 = false;
      }
      if (count == values.Length) {
        break;
      }
    }
  }
}while (count < values.Length);

1 个答案:

答案 0 :(得分:1)

如果您使用的名称具有合理的结构,则可以使用Dictionary将简单键存储到CustomObjects,然后在值上使用枚举器将它们分配给自定义中的Value对象:

var dict = objList.ToDictionary( k => k.Name, v => v);
dict.Dump();

var values = new int[6] {12, 18, 9, 56, 112, 187};
// enumerator that keeps track where we are
var valuesEnumerator = values.GetEnumerator();

// set all to -1
foreach(var v in dict.Values) v.Value =-1;

const int scale = 4;
//group
for(int g = 1;  g <= scale ; g++)
{
   // section
   for(int s = 1;  s <= scale; s++)
   {
      //item
      for(int i = 1; i <= scale; i++)
      {
            // build a key
            var key = String.Format("{0}{1}{2}",i,s,g);
            // check if that key exist
            if (dict.Keys.Contains(key))
            {
                // as long as there numbers in the values array
                if (valuesEnumerator.MoveNext()) 
                {
                    // assign that value
                    dict[key].Value = (int) valuesEnumerator.Current;
                }
            }
      }
   }
}

在我的测试运行中,它返回:

[enter image description here] 1