How to update value of a class property in C#?

300 Views Asked by At

How can I increment the value of the property TeamSize each time a new Athlete object is added? Because I cannot create a variable to store the value for TeamSize and increment that value each time a new Athlete object so I wonder if there is another way to increment TeamSize.

public class Team
{
    private Athlete[] athletes;

    public int MaxTeamSize
    {
        get;
        set;
    }

    public string TeamName
    {
        get;
        set;
    }

    public static int TeamSize
    {
        get;
        set;
    }

    public Sport TeamSport
    {
        get;
        set;
    }

    public Team(string name, Sport sport, int maxTeamSize)
    {
        TeamName = name;
        TeamSport = sport;
        MaxTeamSize = maxTeamSize;
        athletes = new Athlete[maxTeamSize];
    }

    public Athlete[] AddAthlete(Athlete a)
    {
        int teamSize = this.athletes.Length;

        if (teamSize < MaxTeamSize)
        {
            athletes[teamSize] = a;
            teamSize++;
            TeamSize = teamSize;
        }

        return this.athletes;
    }

}

2

There are 2 best solutions below

0
On BEST ANSWER

You don't need to increment. Try this

 public int TeamSize
    {
        get { return athletes.Count(i => i!=null);}
      
    }
public int MaxTeamSize
    {
        get {retun athletes.Length;}
       
    }
 public Team(string name, Sport sport, int maxTeamSize)
    {
        TeamName = name;
        TeamSport = sport;
        athletes = new Athlete[maxTeamSize];
    }

public Athlete[] AddAthlete(Athlete a)
    {
       

        if (TeamSize < MaxTeamSize)
        {
            athletes[TeamSize] = a;
         }

        return this.athletes;
    }

You will need to add using System.Linq; if you don't have. But maybe instead of array of atlets you better use list of atlets. It allows dynamically add any quantity of atlets.

0
On

You have set the TeamSize property as static. It is a critical issue. It cause a side-effect for every instance of Team

    public class Team
    {
        private List<Athlete> athletes;

        public Team(string name, Sport sport, int maxTeamSize)
        {
            TeamName = name;
            TeamSport = sport;
            MaxTeamSize = maxTeamSize;
            athletes = new List<Athlete>(MaxTeamSize);
        }

        public int MaxTeamSize
        {
            get;
            private set;
        }

        public string TeamName
        {
            get;
            set;
        }

        public int TeamSize
        {
            get
            {
                return this.athletes.Count;
            }
        }

        public Sport TeamSport
        {
            get;
            private set;
        }

        public IReadOnlyCollection<Athlete> Athletes
        {
            get
            {
                return athletes;
            }
        }


        public void AddAthlete(Athlete a)
        {

            if (athletes.Count < MaxTeamSize)
            {
                athletes.Add(a);
            }
        }
    }