c# - Assign a list of values to a scaleable custom list of objects -


below example largest set of objects found in list generated elsewhere. there potentially less groups or lower number of values in each group.

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; 

how can edit / create loop or sequence of conditional statements variables array assigned in order described example:

  • set every object's value -1.
  • set value of number of objects (for instance first 6) in following pattern:
    • the first object each group (section 1)
    • then first object of each group (section 2)
    • then second object each group (section 1)
    • finally second object each group (section 2)
    • e.g. 111 -> 211 -> 311 -> 411 -> 121 -> 221 -> 321 -> 421 -> 112 -> ... -> 122 -> etc.

currently, creating array of values in order should assigned regardless of size of cooling list of customobjects. objects in cooling list unordered , can differentiated parsing index in name. if array in example size 6, stop after assigning 221 per example above.

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 code abstracted , array comes part of 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); 

if name use has sensible structure can use dictionary store easy key customobjects , use enumerator on values assign them value in custom object:

var dict = objlist.todictionary( k => k.name, v => v); dict.dump();  var values = new int[6] {12, 18, 9, 56, 112, 187}; // enumerator keeps track var valuesenumerator = values.getenumerator();  // set -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 = 1; <= scale; i++)       {             // build key             var key = string.format("{0}{1}{2}",i,s,g);             // check if key exist             if (dict.keys.contains(key))             {                 // long there numbers in values array                 if (valuesenumerator.movenext())                  {                     // assign value                     dict[key].value = (int) valuesenumerator.current;                 }             }       }    } } 

in test run returns:

[enter image description here]1


Comments