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