作者selfhu (水管爆了)
看板C_Sharp
标题Re: [问题] 请教一个程式技巧
时间Wed Jan 31 02:30:16 2024
我抽取我手边的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://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1706639418.A.989.html
※ 编辑: selfhu (1.169.226.54 台湾), 01/31/2024 02:41:30
1F:→ selfhu: 对了,在add那边不用加[i],读取/写入时才要 01/31 03:10
2F:推 WillBeFine: Thanks a lot! 01/31 09:15