I am stuck on a problem where I am being required to use IComparable, but I'm not sure why, can anyone help me?
This is how the problem has been worded to me
This is the code I have in the main program
and the NamedAnimal class
This is how the problem has been worded to me
You should then ensure that the animals in the displayed list are sorted by the name of the animal using the List Sort method
This is the code I have in the main program
namespace Animals
{
public partial class AnimalForm : Form
{
List<NamedAnimal> animals = new List<NamedAnimal> {new Dog("Doggy"), new Cat("Candy"), new Horse("Horsey"), new Duck("Ducky"), new Cow("Mooo Machine")};
animals.Sort();
public AnimalForm()
{
InitializeComponent();
}
private void AnimalForm_Load(object sender, EventArgs e)
{
for (int i = 0; i < animals.Count; i++)
{
listBoxAnimal.Items.Add(animals.Name);
}
}
private void listBoxAnimal_SelectedIndexChanged(object sender, EventArgs e)
{
// Get selected index
int selection = listBoxAnimal.SelectedIndex;
// Now update display with information from this object
NamedAnimal animal = animals[selection];
pictureBox.Image = animal.Picture;
labelSpecies.Text = animal.Species;
labelSound.Text = animal.Sound;
labelLegs.Text = animal.NumberOfLegs.ToString();
lblnotes.Text = animal.GetNotes();
}
}
}
{
public partial class AnimalForm : Form
{
List<NamedAnimal> animals = new List<NamedAnimal> {new Dog("Doggy"), new Cat("Candy"), new Horse("Horsey"), new Duck("Ducky"), new Cow("Mooo Machine")};
animals.Sort();
public AnimalForm()
{
InitializeComponent();
}
private void AnimalForm_Load(object sender, EventArgs e)
{
for (int i = 0; i < animals.Count; i++)
{
listBoxAnimal.Items.Add(animals.Name);
}
}
private void listBoxAnimal_SelectedIndexChanged(object sender, EventArgs e)
{
// Get selected index
int selection = listBoxAnimal.SelectedIndex;
// Now update display with information from this object
NamedAnimal animal = animals[selection];
pictureBox.Image = animal.Picture;
labelSpecies.Text = animal.Species;
labelSound.Text = animal.Sound;
labelLegs.Text = animal.NumberOfLegs.ToString();
lblnotes.Text = animal.GetNotes();
}
}
}
and the NamedAnimal class
namespace Animals
{
class NamedAnimal : Animal, IComparable<NamedAnimal>
{
string _Name;
public NamedAnimal(string species,
string imageName,
string sound,
int numberOfLegs)
: base(species, imageName, sound, numberOfLegs)
{
_Name = "Unnamed " + species;
}
public NamedAnimal(string name,
string species,
string imageName,
string sound,
int numberOfLegs)
: base(species, imageName, sound, numberOfLegs)
{
_Name = name;
}
public string Name
{
get
{
return _Name;
}
}
public virtual string GetNotes()
{
return "No notes avaliable";
}
public int CompareTo(NamedAnimal otherAnimal)
{
return _Name.CompareTo(otherAnimal.Name);
}
}
}
{
class NamedAnimal : Animal, IComparable<NamedAnimal>
{
string _Name;
public NamedAnimal(string species,
string imageName,
string sound,
int numberOfLegs)
: base(species, imageName, sound, numberOfLegs)
{
_Name = "Unnamed " + species;
}
public NamedAnimal(string name,
string species,
string imageName,
string sound,
int numberOfLegs)
: base(species, imageName, sound, numberOfLegs)
{
_Name = name;
}
public string Name
{
get
{
return _Name;
}
}
public virtual string GetNotes()
{
return "No notes avaliable";
}
public int CompareTo(NamedAnimal otherAnimal)
{
return _Name.CompareTo(otherAnimal.Name);
}
}
}