Re: [問題] 請教一個程式技巧

作者: selfhu (水管爆了)   2024-01-31 02:30:16
我抽取我手邊的code片段給你參考
你再手動加var test = InitSiteActivatorItems();
後續就跟你的很接近了(請恕我沒跑過改成List版,因為會動到現有的WPF code太多)
internal static List<SiteActivatorItems> InitSiteActivatorItems()
{
return new List<SiteActivatorItems>
{
new SiteActivatorItems() { InfName = "Disable", InfNo = -1, Site =
-1, Ready = true },
new SiteActivatorItems() { InfName = "I2C1", InfNo = 0, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "I2C2", InfNo = 1, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "I2C3", InfNo = 2, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "SPI1", InfNo = 3, Site = -1,
Ready = true },
new SiteActivatorItems() { InfName = "SPI2", InfNo = 4, Site = -1,
Ready = true },
new SiteActivatorItems() { InfName = "SPI3", InfNo = 5, Site = -1,
Ready = true },
new SiteActivatorItems() { InfName = "SPI4", InfNo = 6, Site = -1,
Ready = true },
new SiteActivatorItems() { InfName = "UR1", InfNo = 7, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "UR2", InfNo = 8, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "UR3", InfNo = 9, Site = -1,
Ready = false },
new SiteActivatorItems() { InfName = "UR4", InfNo = 10, Site = -1,
Ready = false },
};
}
internal class SiteActivatorItems
{
public string InfName { get; set; }
public int InfNo { get; set; }
public int Site { get; set; }
public bool Ready { get; set; }
}
※ 引述《WillBeFine (努力)》之銘言:
: 大家好..
: 如何將List 宣告成類似以下的名稱,
: 例如:
: List<string> test[0] = new List<string>();
: List<string> test[1] = new List<string>();
: List<string> test[2] = new List<string>();
: 讓我可以用 for loop 的方式根據不同i而將data放在不同的 test[i] List
: 例如:
: int data = 10;
: for (int i=0; i<2; i++)
: {
: test[i].Add(data*i.ToString());
: }
: 或是怎樣可將已宣告的List, 放在像是集合中可以用index的方式被引用??
: 像是
: List<string> test0 = new List<string>();
: List<string> test1 = new List<string>();
: List<string> test2 = new List<string>();
: Set {test0, test1, test2);
: Set[0].Add(data);
: Set[1].Add(data*2);
: Set[2].Add(data*3);
: 謝謝各位..
作者: selfhu (水管爆了)   2024-01-31 03:10:00
對了,在add那邊不用加[i],讀取/寫入時才要
作者: WillBeFine (努力)   2024-01-31 09:15:00
Thanks a lot!

Links booklink

Contact Us: admin [ a t ] ucptt.com