2013-07-30 121 views
2

我是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(); 
      } 
+0

什麼是你'sk'呢? –

+1

在循環的第一行中,您正在向'dataTable'添加一個新行。你不會在這裏覆蓋任何東西。然後你創建一個MyClass對象,填充它,然後**把它扔掉**。 –

+0

這是MyClass的對象。 – user2592968

回答

6

創建一個列表:

var list = new List<MyClass>(); 
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"] }); 

    var sk = new MyClass { 
     Category = match.Groups["C0"].ToString(), 
     Device = match.Groups["C1"].ToString(), 
     Data_Type = match.Groups["C2"].ToString(), 
     Value = match.Groups["C3"].ToString(), 
     Status = match.Groups["C4"].ToString() 
    }; 
    list.Add(sk); 
} 

,那麼你必須在列表中的所有項目。您也可以使用LINQ,例如:

var items = from match in matches 
      select new MyClass { 
       Category = match.Groups["C0"].ToString(), 
       Device = match.Groups["C1"].ToString(), 
       Data_Type = match.Groups["C2"].ToString(), 
       Value = match.Groups["C3"].ToString(), 
       Status = match.Groups["C4"].ToString() 
      }; 

並重復執行items

+0

謝謝你的幫助,這就是我一直在尋找的! – user2592968

1

你需要的是一個列表或東西similer:

List<MyClass> myList = new List<MyClass>(); 
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(); 
     myList.Add(sk); 
} 

在這段代碼結束時,您將有myList在它所有的項目,你可以通過它們與foreach例如

+0

謝謝你的幫助! – user2592968

2

該問題與OOP 本身無關。這與範圍的概念有關。你沒有對你創建的sk對象做任何事情。因此,當你的代碼碰到循環體的底部時,sk就會被丟棄。

也許你打算存儲在列表中的對象的引用:

//Create a list to store your new shiny sk objects 
List<MyClass> sks = new List<MyClass>(); 

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(); 

    //Add the object to your list 
    sks.Add(sk); 
} 
3

對象SK在循環的每個迭代是走出去的範圍。

嘗試這樣的事情,如果你想保持他們的列表:

var list = new List<MyClass>(); 
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(); 
    list.Add(sk); 
} 
+0

這也是一個很好的答案。謝謝! – user2592968