【问题标题】:Creating list/array of class instances?创建类实例的列表/数组?
【发布时间】:2013-12-02 07:28:01
【问题描述】:

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

这是我现在创建的课程:

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),然后通过他们的团队编号在程序中的所有实例中找到一个特定的团队?

【问题讨论】:

  • 您使用的是 Windows 窗体吗?此外,当您将团队添加到组合框时,您是尝试通过用户选择来定位项目还是要通过代码定位项目?除了团队编号之外,组合中还会出现什么?

标签: c# arrays list class search


【解决方案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];

【讨论】:

    【解决方案2】:
    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】:

      您是否尝试过创建列表?

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

      【讨论】:

        【解决方案4】:

        像这样:

        为你的类添加一个构造函数来获取团队编号:

        (如果每个团队都需要一个号码,这是最好的解决方案。所以你不能忘记设置团队号码,因为如果不在构造函数中设置号码就无法创建团队类型的对象 em>)

        public class Team
        {
            protected int _teamNum;
            public Team(int teamNum)
            {
                _teamNum = teamNum;
            }
        
            public int getTeamNum()
            {
                return _teamNum;
            }
        
              //more logic
            }
        

        填充dictionary,组合框并为其编号获取一个团队:

            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
            }
        

        【讨论】:

          猜你喜欢
          • 2015-11-19
          • 2016-05-09
          • 2017-05-31
          • 2014-04-21
          • 1970-01-01
          • 2011-09-11
          • 2019-05-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多