I'm having some problem with this method of mine. Somehow, I doesn't create or doesn't accept the created object (animal). It seems it only return null.
public Animal GetSetAnimalInfo(string name, double age, CategoryType cat, object animalType, GenderType gender, string extra)
{
Animal animalObj = null;
switch (cat)
{
case CategoryType.Bird:
Bird.BirdSpecies birdSpecie = (Bird.BirdSpecies)Enum.Parse(typeof(Bird.BirdSpecies), animalType.ToString());
animalObj = Bird.BirdFactory.CreateBird(birdSpecie);
break;
case CategoryType.Insect:
Insect.InsectSpecies insectSpecie = (Insect.InsectSpecies)Enum.Parse(typeof(Insect.InsectSpecies), animalType.ToString());
animalObj = Insect.InsectFactory.CreateInsect(insectSpecie);
break;
case CategoryType.Mammal:
Mammal.MammalSpecies mammalSpecie = (Mammal.MammalSpecies)Enum.Parse(typeof(Mammal.MammalSpecies), animalType.ToString());
animalObj = Mammal.MammalFactory.CreateMammal(mammalSpecie);
break;
case CategoryType.Marine:
Marine.MarineSpecies marineSpecie = (Marine.MarineSpecies)Enum.Parse(typeof(Marine.MarineSpecies), animalType.ToString());
animalObj = Marine.MarineFactory.CreateMarine(marineSpecie);
break;
case CategoryType.Reptile:
Reptile.ReptileSpecies reptileSpecie = (Reptile.ReptileSpecies)Enum.Parse(typeof(Reptile.ReptileSpecies), animalType.ToString());
animalObj = Reptile.ReptileFactory.CreateReptile(reptileSpecie);
break;
}
if (animalObj != null)
{
System.Windows.Forms.MessageBox.Show(animalType.ToString());
}
if (!InputUtility.IsLettersOnly(name))
{
string message = string.Format("Animal could not be registered because the name input was not correct.") + Environment.NewLine;
throw new InvalidInputException(message);
}
else
{
animalObj.Name = name;
}
animalObj.ID = GenerateID(animalObj);
if (!InputUtility.IsNumeric(age))
{
string message = string.Format("Animal could not be registered because the age input was not correct." + Environment.NewLine);
throw new InvalidInputException(message);
}
else
{
animalObj.Age = age;
}
animalObj.Gender = gender;
animalObj.ExtraAnimalInfo = extra;
AddToList(animalObj);
return animalObj;
}
This is the second method, which is being used for the GUI whereas the first one handles the data, and creation of objects. However, as mentioned above, somehow the first method returns null
private Animal AddAnimal()
{
Animal animalObj = null;
try
{
animalManager.GetSetAnimalInfo( txtName.Text,
Convert.ToDouble(txtAge.Text),
(CategoryType)lstCategory.SelectedIndex,
lstAnimalType.SelectedValue,
(GenderType)lstGender.SelectedIndex,
txtSpecialInput.Text);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
if (animalObj != null)
{
try
{
ListViewItem lvi = new ListViewItem(animalObj.Name);
lvi.SubItems.Add(animalObj.ID);
lvi.SubItems.Add(animalObj.Age.ToString());
lvi.SubItems.Add(animalObj.Gender.ToString());
lvi.SubItems.Add(animalObj.Category.ToString());
lstVRegisteredAnimals.Items.Add(lvi);
}
catch (FormatException ex) { MessageBox.Show(ex.Message); }
}
return animalObj;
}