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

看板C_Sharp (C#)作者 (水管爆了)時間11月前 (2024/01/31 02:30), 11月前編輯推噓1(101)
留言2則, 2人參與, 11月前最新討論串2/2 (看更多)
我抽取我手邊的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); : 謝謝各位.. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.169.226.54 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1706639418.A.989.html ※ 編輯: selfhu (1.169.226.54 臺灣), 01/31/2024 02:41:30

01/31 03:10, 11月前 , 1F
對了,在add那邊不用加[i],讀取/寫入時才要
01/31 03:10, 1F

01/31 09:15, 11月前 , 2F
Thanks a lot!
01/31 09:15, 2F
文章代碼(AID): #1bkK0wc9 (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #1bkK0wc9 (C_Sharp)