Click here to Skip to main content
15,746,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I'm doing a univerity C# assignment that involves an animal motel...

I'll skip the details and cut to the chase..I'm havin problems with the input..I'm not sure how to handle the specific animal input like wingspan for birds, number of teeths for mammals?
this is a chunk of the code from my mainform class:

private void AddAnimal(){

        Animal animalObj;

            Category animalCategory = (Category)CategoryList.SelectedIndex;

            switch (animalCategory)
                case Category.Mammal:
                    MammalSpecies animalSpecies = (MammalSpecies) Enum.Parse(typeof (MammalSpecies), AnimalList.Text);
                    animalObj = MammalFactory.CreateMammal(animalSpecies);

I was thinking of seomthing like this
C# = txtName.Text;

but realised I cant handle the specific animal input like this only the general like name age etc..

can someone please point me in the right direction I'm confused

The way I would do it is to start by separating your concerns.
Start by removing all reverences to controls and other UI elements from your "Add Animal" method and pass in the appropriate data as parameters. Then return the Animal object from the method when you have created it.

So your method would become
private Animal AddAnimal( string animalName, Category category)
That way, you are separating stuff that changes a lot (the UI) from stuff that doesn't change much (Animals)

Then, I'd look at creating an Abstract Animal class, and deriving categories from that, such as Mammal, Reptile, and so on. Possibly, you will want to make those abstract as well and derive Feline, Bovine, etc from Mammal - but that may be for later.
That way, you can start decomplicating your constructor:
private Animal AddAnimal( string animalName, Category category)
   switch (category)
      case Category.Mammal: return new Mammal(animalName);
      case Category.Reptile: return new Reptile(animalName);

Does that make sense, or is it a bit further than your course has got yet?
Share this answer
[no name] 2-Oct-15 8:53am    
We haven't really gotten to abstract classes yet..this is only the first's limited to 2 species and each species has 2 subclasses....
I also have the classes mammalfactory and birdfactory for creating the objects
[no name] 4-Oct-15 4:32am    
How would you do it without the Abstract animal class?
OriginalGriff 4-Oct-15 5:11am    
The problem is that without an Animal base class of some kind you can't declare a variable as Animal and then assign it a Mammal, or a Bird - both of those two classes would have to be derived from Animal in order to do that.

You could create Animal as an ordinary class and then derive Mammal and Bird from that and that would work. You know how to do that? (Sorry, I have no idea how far into your course you are, or what has been covered so far, so bear with me if I have to ask these things - I don't want to confuse you by giving you help to create something you haven't covered and aren't supposed to know about yet! :laugh:)
[no name] 4-Oct-15 8:31am    
I already have an Animal base class with two derived classes: mammal and bird which both have two derived classes of their own cat and dog, eagle and falcon. I just didn't want to paste to much code in here lol...
I have basiclly done most of the work on the assigment but I cant wrap my head around how to handle the inputs when creating the animal i.e the specific characteristics for each animal..
they only thing we still haven't covered is abstract classes which I believe is the next assignment
OriginalGriff 4-Oct-15 8:41am    
In which case ignore the "abstract" bit (which just makes things easier later)
The AddAnimal method in my answer will still work, just as it is.

Because Mammal and Bird are derived from Animal, an Animal variable can hold either of them:

Animal a = new Mammal("Elephant");
a = new Bird("Ostrich");

Will compile fine.
If you add a Category property to your Animal, you can check what type it is (without having to try casting it when you want to use it):

Animal a = new Mammal("Elephant");
if (a.Category == Category.Mammal) ...

Since you have derived classes as well:

Animal a = new Cat("Tiddles");

Will also work.

If you override ToString (Do you know how to do that yet?) in each of the derived classes, then this:

Animal a = new Cat("Tiddles");
a = new Falcon("Freddy the Raptor");

Could print

"Cat: Tiddles"
"Falcon: Freddy the Raptor"
Usually polymorphism[^] is the object oriented programming repacement of the switch control statement.
Share this answer
You can try the PropertyGrid[^] class.

For example, you have a variable called propertyGrid1. Then you assign your current object the property SelectedObject.
propertGrid1.SelectedObject = animalObj;

All the public properties of the current class will show up in the property grid.

You can also make it more pretty by adding attributes such as DescriptionAttribute[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900