C#类对象覆盖

时间:2013-07-30 08:17:27

标签: c# winforms visual-studio-2010

我是OOP的新手,所以请考虑到这一点。我把从这个匹配中得到的数据放到一个类的对象中,但它是在foreach循环中完成的,所以每次调用它时,我的对象中的数据都会被覆盖,最后我想把所有数据放在我的宾语。但我只是从上一场比赛开始。我该怎么做才能避免这种覆盖?也许我是以完全错误的方式做到的?

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
}

答案 2 :(得分: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 :(得分:1)

你需要的是List或类似的东西:

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进行审核,例如

相关问题