创建类实例的列表/数组?

时间:2013-11-18 22:58:30

标签: c# arrays list class search

我对C#很新,我刚学会了创建自定义类。问题是,我无法弄清楚如何使用此类的 40~65 实例并将它们放入列表/数组(我需要的任何一个)中,我可以找到并选择一个基于其中定义的属性。

这是我现在创建的课程:

public class Team
{
    protected int teamNum;
    protected double averageMatchPoints;
    protected string location;
    protected int matchesPlayed;
    protected int matchesPending;
    protected int blowouts;

    //Team Number
    public void SetNumber(int num)
    {
        teamNum = num;
    }

    public int GetNumber()
    {
        return teamNum;
    }

    //Average Points per match
    public void AverageMatchPoints(double p)
    {
        averageMatchPoints = p;
    }

    public double GetAverageMatchPoints()
    {
        return averageMatchPoints;
    }

    //location information
    public void SetLocation(string l)
    {
        location = l;
    }

    public string GetLocation()
    {
        return location;
    }

    //Number of Played Matches
    public void PlayedMatches(int mat)
    {
        matchesPlayed = mat;
    }

    public int GetPlayedMatches()
    {
        return matchesPlayed;
    }

    //Number of matches pending (not played)
    public void PendingMatches(int pen)
    {
        matchesPending = pen;
    }

    public int GetPendingMatches()
    {
        return matchesPending;
    }

    //Number of Blowouts (matches where the robot was disbaled for any number of reasons)
    public void SetBlowouts(int b)
    {
        blowouts = b;
    }

    public int GetBlowouts()
    {
        return blowouts;
    }
}

现在,如果我有 40~65 这些团队在一个活动中竞争,我为每个人制作了这个类的实例,我将如何在每个团队编号中填充一个组合框(teamNum),然后通过团队编号在程序中的所有实例中找到一个特定的团队?

4 个答案:

答案 0 :(得分:1)

我推荐一本字典!

// Declared somewhere
private Dictionary<int, Team> _teamDictionary = new Dictionary<int, Team>();
.
.
.
//Initialization code - I assume you have gotten your teams from a database or somewhere?
foreach (var team in myTeamsList)
{
    _teamDictionary.Add(team.teamNum, team);
}
.
.
.
// Later when you want to locate a team:
var team = _teamDictionary[selectedTeamNum];

答案 1 :(得分:1)

你有没有尝试创建一个列表?

List<Team> Teams { get; set; }

然后,您可以将组合框绑定到您拥有的所有团队的列表/集合/ IEnumerable。要将团队初始化为40/60,请执行以下操作?

for(int i = 0; i < 60; i++)
{
Team t = new Team();
t.Name = "Team 1";
t.TeamNumber = i + 1;
Teams.Add(t);
}

答案 2 :(得分:0)

List<Team> allTheTeams = new List<Team>();

for(var i = 0; i < 65; i++){
  allTheTeams.Add(new Team { teamNum = i });
}

让团队获得第34名:

allTheTeams.FirstOrDefault(x => x.teamNum == 34);

答案 3 :(得分:0)

像这样:

在您的类中添加一个构造函数,该构造函数包含teamnumber:

如果每个团队都需要一个号码,这是最好的解决方案。所以你不能忘记设置团队号码,因为你不能在没有在构造函数中设置数字的情况下创建团队类型的对象

public class Team
{
    protected int _teamNum;
    public Team(int teamNum)
    {
        _teamNum = teamNum;
    }

    public int getTeamNum()
    {
        return _teamNum;
    }

      //more logic
    }

填充dictionary,comboBox并获取其编号的团队:

    Dictionary<int, Team> dictionary = new Dictionary<int, Team>();

    int teamNum = 1;
    // Add your Teams to a dictionary (example)
    dictionary.Add(teamNum ,new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));

    // Populate a comboBox
    foreach(KeyValuePair<int,Team> kvp in dictionary)
    {
        comboBox1.Items.Add(kvp.Value.getTeamNum().ToString());
    }

    // get a team for a given teamNumer
    int targetTeamNumber = 2;
    if (dictionary.ContainsKey(targetTeamNumber))
    {
        Team team = dictionary[targetTeamNumber];
        // do something with the team
    }