我是OOP的新手,所以請考慮這一點。我將從匹配中獲得的數據放入類的對象中,但是它在foreach循環中完成,因此每次調用時都會覆蓋對象中的數據,最後我希望將所有數據放入我的目的。但我只有從最後一場比賽。我應該怎麼做才能避免這種覆蓋?也許我以完全錯誤的方式去做?C#類對象覆蓋
foreach (var match in matches)
{
dataTable.Rows.Add(new Group[] { match.Groups["C0"], match.Groups["C1"], match.Groups["C2"], match.Groups["C3"], match.Groups["C4"] });
MyClass sk = new MyClass();
sk.Category = match.Groups["C0"].ToString();
sk.Device = match.Groups["C1"].ToString();
sk.Data_Type = match.Groups["C2"].ToString();
sk.Value = match.Groups["C3"].ToString();
sk.Status = match.Groups["C4"].ToString();
}
什麼是你'sk'呢? –
在循環的第一行中,您正在向'dataTable'添加一個新行。你不會在這裏覆蓋任何東西。然後你創建一個MyClass對象,填充它,然後**把它扔掉**。 –
這是MyClass的對象。 – user2592968