T
Tony Johansson
Hello!
This Add method below is called when I want to add an animal to the animal
collection that is located in the AnimalManager class.In the ValidateData
method that is called from the Add method is used for checking some business
rules.
In this method ValidateData I use the GetType to get the dynamic type but
somebody of you mentioned that it was not appropriate to do in the way that
I now do but instead should use a property. It was especially Peter and Arne
that was mentioned this and I hope they will read this thread.
This animal parameter that is passed to this Add method is comming from a
selection in a ListBox in the GUI class.
So I just want to know how the implementation of this property should look
like.
animalManager.Add(category, animal, txtName.Text,
double.Parse(txtAge.Text.Trim()), gender, housing, eater, false,
foodMenuList);
public void Add(CategoryType category, string animal, string name, double
age, GenderType gender,HousingType housing,
EaterType eater, bool aggressive, List<string>
foodMenuList)
{
//Check some business rules
ValidateData(name, animal);
//Generate the ID that is unique each animal in the collection
string ID = GenerateID();
switch (category)
{
case CategoryType.Mammal:
MammalSpices mammalSpecies =
(MammalSpices)Enum.Parse(typeof(MammalSpices), animal);
animals.Add( (Animal)
MammalFactory.CreateMammal(mammalSpecies, name,ID, age, gender, housing,
eater, false, foodMenuList));
break;
case CategoryType.Bird:
BirdSpices
birdSpecies=(BirdSpices)Enum.Parse(typeof(BirdSpices),animal);
animals.Add((Animal)BirdFactory.CreateBird(birdSpecies, name,
ID, age,gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Insect:
InsectSpecies insectSpecies =
(InsectSpecies)Enum.Parse(typeof(InsectSpecies),animal);
animals.Add((Animal)InsectSpecies.CreateBird(insectSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Marine :
MarineSpices marineSpecies =
(MarineSpices)Enum.Parse(typeof(MarineSpices), animal);
animals.Add((Animal)MarineSpices.CreateBird(marineSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Reptile :
ReptileSpecies reptileSpecies =
(ReptileSpecies)Enum.Parse(typeof(ReptileSpecies), animal);
animals.Add((Animal)ReptileSpecies.CreateBird(reptileSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
}
}
//Check the business rules that say there must not be two names for the same
animalType
private void ValidateData(string name, string animal)
{
Animal thisAnimal = (Animal)animals.Find(delegate(Animal item)
{
return (string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase) &&
string.Equals(item.GetType().Name, animal,
StringComparison.CurrentCultureIgnoreCase));
});
if (thisAnimal != null)
throw new ArgumentException("Duplicate name for same animalType
is not allowed");
}
//Tony
This Add method below is called when I want to add an animal to the animal
collection that is located in the AnimalManager class.In the ValidateData
method that is called from the Add method is used for checking some business
rules.
In this method ValidateData I use the GetType to get the dynamic type but
somebody of you mentioned that it was not appropriate to do in the way that
I now do but instead should use a property. It was especially Peter and Arne
that was mentioned this and I hope they will read this thread.
This animal parameter that is passed to this Add method is comming from a
selection in a ListBox in the GUI class.
So I just want to know how the implementation of this property should look
like.
animalManager.Add(category, animal, txtName.Text,
double.Parse(txtAge.Text.Trim()), gender, housing, eater, false,
foodMenuList);
public void Add(CategoryType category, string animal, string name, double
age, GenderType gender,HousingType housing,
EaterType eater, bool aggressive, List<string>
foodMenuList)
{
//Check some business rules
ValidateData(name, animal);
//Generate the ID that is unique each animal in the collection
string ID = GenerateID();
switch (category)
{
case CategoryType.Mammal:
MammalSpices mammalSpecies =
(MammalSpices)Enum.Parse(typeof(MammalSpices), animal);
animals.Add( (Animal)
MammalFactory.CreateMammal(mammalSpecies, name,ID, age, gender, housing,
eater, false, foodMenuList));
break;
case CategoryType.Bird:
BirdSpices
birdSpecies=(BirdSpices)Enum.Parse(typeof(BirdSpices),animal);
animals.Add((Animal)BirdFactory.CreateBird(birdSpecies, name,
ID, age,gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Insect:
InsectSpecies insectSpecies =
(InsectSpecies)Enum.Parse(typeof(InsectSpecies),animal);
animals.Add((Animal)InsectSpecies.CreateBird(insectSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Marine :
MarineSpices marineSpecies =
(MarineSpices)Enum.Parse(typeof(MarineSpices), animal);
animals.Add((Animal)MarineSpices.CreateBird(marineSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
case CategoryType.Reptile :
ReptileSpecies reptileSpecies =
(ReptileSpecies)Enum.Parse(typeof(ReptileSpecies), animal);
animals.Add((Animal)ReptileSpecies.CreateBird(reptileSpecies,
name, ID, age, gender, housing, eater, false,
foodMenuList));
break;
}
}
//Check the business rules that say there must not be two names for the same
animalType
private void ValidateData(string name, string animal)
{
Animal thisAnimal = (Animal)animals.Find(delegate(Animal item)
{
return (string.Equals(item.Name, name,
StringComparison.CurrentCultureIgnoreCase) &&
string.Equals(item.GetType().Name, animal,
StringComparison.CurrentCultureIgnoreCase));
});
if (thisAnimal != null)
throw new ArgumentException("Duplicate name for same animalType
is not allowed");
}
//Tony