我有一个应该显示一些标签的程序(10-50)。 我希望将值存储在数组中,以便迭代它。 我发现了如何在ArrayList中存储不同的数据类型。 我有一个类hdinfoData,我将它分配给一个数组列表。 我以为我有类似于具有不同数据类型的2D数组。 我无法访问存储在数组列表中的值,因为它实际上不是2D数组,它只是一个包含对象的数组(hdinfoData类)
这是正确的方法吗? 我想创建一对标签" Label" "值"动态。 此外,我想改变顺序。
ArrayList hdiData = new ArrayList();
hdinfoData a = new hdinfoData();
a.ID = 1;
a.Label = "USER";
a.Value = user;
a.PosX = 1;
a.PosY = 2;
hdiData.Add(a);
hdinfoData b = new hdinfoData();
b.ID = 2;
b.Label = "HOST";
b.Value = host;
hdiData.Add(b);
Label[] lbl = new Label[hdiData.Count];
int y = 50;
for (int i = 0; i < hdiData.Count; i++)
{
int x = 15;
lbl[i] = new Label();
lbl[i].Location = new System.Drawing.Point(x, y);
lbl[i].Text = (string)hdiData[i];
lbl[i].ForeColor = Color.White;
Form1.Controls.Add(lbl[i]);
y += 25;
}
internal class hdinfoData
{
public int ID { get; set; }
public string Label { get; set; }
public string Value { get; set; }
public int PosX { get; set; }
public int PosY { get; set; }
}
答案 0 :(得分:1)
您最好使用List<hdinfoData>
代替ArrayList
。然后只需访问其元素&#39;字段与任何其他对象字段一样:
var hdiData = new List<hdinfoData>();
.................
lbl[i].Text = hdiData[i].Label;