将值列表分配给可扩展的自定义对象列表
Assign a list of values to a scaleable custom list of objects
下面是我在其他地方生成的列表中可以找到的最大对象集的示例。每个组中可能有更少的组或更少的值。
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 部分)
- THEN 每组的第一个对象(第 2 部分)
- 然后回到每个组的第二个对象(第 1 部分)
- 最后是每个组中的第二个对象(第 2 部分)
- 例如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);
如果您使用的名称具有合理的结构,您可以使用 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;
}
}
}
}
}
在我的测试中 运行 这个 returns:
]1
下面是我在其他地方生成的列表中可以找到的最大对象集的示例。每个组中可能有更少的组或更少的值。
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 部分)
- THEN 每组的第一个对象(第 2 部分)
- 然后回到每个组的第二个对象(第 1 部分)
- 最后是每个组中的第二个对象(第 2 部分)
- 例如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);
如果您使用的名称具有合理的结构,您可以使用 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;
}
}
}
}
}
在我的测试中 运行 这个 returns: