0

为了加深我自己的理解,我正在复制一个简单的数据库 - 并且无法理解以下内容;

我有 2 个类城镇和人。一个城镇拥有许多 People 实例,并且设置如下;

    public class Town
{
    List<People> collectionOfPeople;

    public string townName { get; set; }

    public Town()
    {
        townName = "Cardiff";

        collectionOfPeople = new List<People>();
        collectionOfPeople.Add(new People("Daniel Smith"));
    }
}

    public class People
{

    public string name { get; set; }

    public People(string tmp_name)
    {
        name = tmp_name;
    }
}

假设我所做的是正确的,Town 有 1 个值(卡迪夫),而 People 也有 1 个(Daniel Smith)或 .. Daniel 住在卡迪夫。

我正在尝试显示居住在镇内的人们的名字.. 以便以后循环浏览它们。(** = 我认为的问题)

    private List<Town> townList;
    private List<Town.People> peopleList;              **

    private void ShowData()
    {
        // Add to Text Box based on current Record
        txt_town.Text = townList[0]).townName;
        txt_name.Text = peopleList[0].name;            **
    }
4

2 回答 2

1

这是我的更改。为 Town 类中的 collectionOfPeople 提供公共访问修饰符。

public class Town
{
    public List<People> collectionOfPeople;
    public string townName { get; set; }
}

之后,您可以访问 Town 内的 People 实例。像这样的东西:

private List<Town> townList = new List<Town>();    
private void ShowData()
{
    // Add to Text Box based on current Record
    txt_town.Text = townList[0].townName;
    txt_name.Text = townList[0].collectionOfPeople[0].name
}
于 2013-04-15T19:24:50.073 回答
0

你还没有说你收到了什么错误信息,如果有的话,但我相信为了有

Town.People

您需要创建一个名为 People 的 Town 类的属性。我在你的代码中没有看到。

此外,您的行中还有一个额外的括号:

txt_town.Text = townList[0].townName: //no ) after [0]
于 2013-04-15T19:16:56.423 回答