2013-07-30 118 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