I'm working on a project where the user is inputing values to animal (Name, age, gender etc..) and the values entered by the user are displayed on a list-box The classes inherit from each other. Here is how the inheritance work:
Animal class is the parent for all classes.
Mammal class inherit from Animal class.
Dog class inherit from Mammal class.
Cat class inherit from Mammal class
Reptile class inherit from Animal class
Snake class inherit from Reptile class
Lizard class inherit from Reptile class
The variables for Animal is id, name, age, gender.
The variable for Mammal is teeth
The variable for Dog is barkLevel
The user is able to choose what animal to create. There is one listbox showing the type of animal (Mammal and Reptile) and there is a listbox next to it that depending on the type of animal selected by the user, it displays its animals.
For exampel, if the user selects Mammal in the listbox, the listbox next to it displays Dog and Cat.
This is working perfectly and it's not the problem.
The problem:
The input values are being displayed on the result list, but not the child class variables. The result listbox is displaying Id, Name, Age, Gender but not teeth or barkLevel. There are no error messages or anything. It just doesn't display the child class variables.
Here is my MainForm:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assign_1
{
public partial class MainForm : Form
{
private Dog m_dog = new Dog();
private Cat m_cat = new Cat();
private Snake m_snake = new Snake();
private Lizard m_lizard = new Lizard();
private AnimalManager animalmgr = null;
private Animal m_animal = new Animal();
private Mammal m_mammal = new Mammal();
public MainForm()
{
//Visual Studio initializations
InitializeComponent();
//My initializations
InitializeGUI();
Gendercmb.DataSource = Enum.GetValues(typeof(GenderType));
Categorylst.DataSource = Enum.GetValues(typeof(Categorytype));
animalmgr = new AnimalManager();
}
private void InitializeGUI()
{
ReadInput();
}
private void ReadInput()
{
m_animal.Name = ReadName();
m_animal.Age = ReadAge();
m_mammal.Teeth = ReadTeeth();
m_dog.BarkLevel = ReadBarklevel();
m_animal.Gender = this.Gendercmb.GetItemText(this.Gendercmb.SelectedItem);
}
private int ReadAge()
{
int age = 0;
int.TryParse(Agetxt.Text, out age);
return age;
}
private int ReadBarklevel()
{
int bark = 0;
int.TryParse(barktxt.Text, out bark);
return bark;
}
private int ReadTeeth()
{
int teeth = 0;
int.TryParse(teethtxt.Text, out teeth);
return teeth;
}
private string ReadName()
{
string name = "";
name = Nametxt.Text;
return name;
}
private void addMammal(Mammal values)
{
ReadInput();
switch ((MammalType)Animallst.SelectedIndex)
{
case MammalType.Dog:
{
// Use a copy constructor to set a dog with common data
Dog m_dog = new Dog(values);
// If more data in GUI to fill in for this animal, do it here
//Then send it to the manager for adding to the list
animalmgr.add(m_dog);
break;
}
case MammalType.Cat:
{
Cat m_cat = new Cat(values);
animalmgr.add(m_cat);
break;
}
}
}
private void AddReptile(Reptile values)
{
ReadInput();
switch ((ReptileType)Animallst.SelectedIndex)
{
case ReptileType.Snake:
{
// Use a copy constructor to set a snake with common data
Snake m_snake = new Snake(values);
// If more data in GUI to fill in for this animal, do it here
//Then send it to the manager for adding to the list
animalmgr.add(m_snake);
break;
}
case ReptileType.Lizard:
{
Lizard m_lizard = new Lizard();
animalmgr.add(m_lizard);
break;
}
}
}
//When user clicks "Add to list"
private void button1_Click(object sender, EventArgs e)
{
ReadInput();
System.Windows.Forms.MessageBox.Show(m_dog.BarkLevel.ToString());
switch ((Categorytype)Categorylst.SelectedIndex)
{
case Categorytype.Mammal:
{
Mammal mammal = new Mammal(m_animal);
addMammal(mammal);
break;
}
case Categorytype.Reptile:
{
Reptile m_reptile = new Reptile(m_animal);
AddReptile(m_reptile);
break;
}
}
UpdateResults();
}
private void UpdateResults()
{
Resultlst.Items.Clear(); //Erase current list
//Get one elemnet at a time from manager, and call its
//ToString method for info - send to listbox
for (int index = 0; index < animalmgr.ElementCount; index++)
{
Animal animal = animalmgr.GetElementAtPosition(index);
toString method.
Resultlst.Items.Add(animal.ToString()); // <--- Here is the problem
}
}
}
}
Here is my Animal manager class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign_1
{
class AnimalManager
{
// private List<Animal> m_animalList;
private List<Animal> m_animal;
private List<Mammal> m_mammal;
public AnimalManager()
{
//In this list objects off diff animals of all species are saved
// m_animalList = new List<Animal>();
m_animal = new List<Animal>();
m_mammal = new List<Mammal>();
}
public void add(Animal ObjIn)
{
m_animal.Add(ObjIn);
}
public bool IsIndexValid(int index)
{
return ((index >= 0) && (index < m_animal.Count));
}
public Animal GetElementAtPosition(int index)
{
//We choose to return a copy
if (IsIndexValid(index))
{
if (m_animal[index] is Mammal)
return new Mammal((Mammal)m_animal[index]);
if (m_animal[index] is Reptile)
return new Reptile((Reptile)m_animal[index]);
return null;
}
else
return null;
}
public int ElementCount
{
get { return m_animal.Count; }
}
}
}
UPDATE:
I know now that the problem is Resultlst.Items.Add(animal.ToString()); Somehow I need to include the mammal and dog object (depending on the selected animal) but I don't know how.
From what I can tell you're never actually returning your sub classes. Assuming you've got all your cats, dogs, snakes and lizards in m_animal, when you run them through the base class constructors you are actually losing the properties of the sub classes. Try returning the objects directly:
public Animal GetElementAtPosition(int index)
{
if (IsIndexValid(index))
return m_animal[index];
else
return null;
}
Provided you have ToString() overridden in all your classes you should be getting the result you're expecting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With