I currently have a problem where my program should show parameters from each class onto the form, however the class lowest in the hierarchy isn’t showing. For example; I want it to show “Charity Cyclist No. 1 riding a tricycle with 3 wheels and £300 sponsorship” but it’s only showing “Charity Cyclist No. 1 riding a tricycle with 3 wheels”.
Here is my Sponsorship class:
class Sponsorship : CharityCyclists
{
private double fundsRaised;
public double FundsRaised
{
get { return fundsRaised; }
set { fundsRaised = value; }
}
public Sponsorship(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, bicycle, wheels, fundsRaised)
{
this.fundsRaised = fundsRaised;
}
public override string ToString()
{
return base.ToString() + " and sponsorship amount: " + fundsRaised;
}
}
Here is my CharityCyclists class:
class CharityCyclists : Cyclists
{
private string bicycle;
private int wheels;
public string Bicycle
{
get { return bicycle; }
set { bicycle = value; }
}
public int Wheels
{
get { return wheels; }
set { wheels = value; }
}
public CharityCyclists(string type, int number, string finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs)
{
this.bicycle = bicycle;
this.wheels = wheels;
}
public override string ToString()
{
return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels";
}
}
and my Cyclists class:
public static int NumFinished
{
get { return Cyclists.numFinished; }
set { Cyclists.numFinished = value; }
}
public string Type
{
get { return type; }
set { type = value; }
}
public int Number
{
get { return number; }
set { number = value; }
}
public string Finished
{
get { return finished; }
set { finished = value; }
}
public int Hours
{
get { return hours; }
set { hours = value; }
}
public int Mins
{
get { return mins; }
set { mins = value; }
}
public int Secs
{
get { return secs; }
set { secs = value; }
}
public Cyclists(string type, int number, string finished, int hours, int mins, int secs)
{
this.type = type;
this.number = number;
this.finished = finished;
this.hours = hours;
this.mins = mins;
this.secs = secs;
}
public override string ToString()
{
return type + " No. " + number;
}
public Cyclists()
{
}
This is where they are added to the listbox on the form:
Cyclists[] cyclistsList;
cyclistsList = new Cyclists[2];
cyclistsList[0] = new CharityCyclists("Novelty Charity Cyclist", 1, "Not Finished", 0, 0, 0, "Tricycle", 2, 300);
cyclistsList[1] = new CompetitiveCyclists("Competitive Cyclist", 1, "Not Finished", 0, 0, 0, 25, 2);
for (int i = 0; i < MAX_CYCLISTS; i++)
{
lstCyclists.Items.Add(cyclistsList[i]);
}
Thanks!
You are not creating Sponsorship instances at all. At this moment you are calling ToString() override of a CharityCyclist class, not Sponsorship because of this constructor call:
cyclistsList[0] = new CharityCyclists("Novelty Charity Cyclist", 1, "Not Finished", 0, 0, 0, "Tricycle", 2, 300);You should modify your code like this:It is about polymorphism, you need to make an instance of the class which overridden method you would like to be called.