Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I'm having an issue trying to architect a program, and am looking for suggestions.

I'll use examples to try to illustrate what I'm trying to accomplish.

I have a base class "Animal" that stores properties that every animal has:

C#
public class Animal: INotifyPropertyChanged
{
    #region PropertyChanged Event
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion

    private string name;
    private string gender;

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    public string Gender
    {
        get { return gender; }
        set
        {
            gender = value;
            OnPropertyChanged("Gender");
        }
    }
}


I then have other classes that implement the base class animal:

C#
public class Dog: Animal
{
    // Insert Dog Specific Properties and Methods Here
}


C#
public class Skunk: Animal
{
    // Insert Skunk Specific Properties and Methods Here
}


I want to have one object that I can use to pass all of the base class information, as well as derived class information. Right now the only way I can think to do this would be with another class that is just a container of all animals:

C#
public class AnimalContainer
{
    Dog dog = new Dog();
    Skunk skunk = new Skunk();
}


Is there a better way to accomplish what I am trying to do?
Posted
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 18:21pm    
Not clear, hence -- my answer. Not just a joke... :-)
--SA

C#
public class AnimalCollection : List<Animal>
{
}

You can add any animal-derived object to thie collection.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 18:19pm    
I confirm it and credit in my answer, please see. :-) My 5.

However, the problem is: OP does not seem to clearly understand what he wants. Polymorphous container is just the most apparent thing which comes to mind.
--SA
Though i'm not sure what the question is i'll give it a try anyway;

If you create a new Dog dog you can pass the dog to any other method as a Animal thus
 Public void DoSomething(Animal animal)
{
...
}


Then when you actually go and do something with the animal (damn that sounds dirty) all you have to do is
Dog dog = animal as Dog;
if(null != dog)
{
   Console.WriteLine("this is a real dog")
...


It would even be better if you'd make a Animal interface and pass that to the user method.

Hope this helps, AT
 
Share this answer
 
Comments
Adam7869 9-Dec-11 10:13am    
Thanks Addy, this is what I was looking for. I thought that once I cast my Dog as an Animal, all of the Dog specific properties were gone. But I tested what you wrote and it worked!

Is there any difference (and advantages or disadvantages) between using "Dog dog = animal as Dog" or "Dog dog = (Dog)animal"?

Thanks Again.
Addy Tas 9-Dec-11 14:41pm    
When Casting a non dog (e.g. Cat) to a dog you can expect a casting exception, using the as operator will make the result null if it's not a dog. However the as operator does not come cheap, as always safety comes at a price. Regular casting is really cheap but then again risking a exception might make you app unusable... I'd say only cast if the object really can't be anything but the expected type, if you happen to see an exception anyway something else is wrong in your app.

Cheers, AT
Addy Tas 9-Dec-11 14:44pm    
Next to the basics of inheritance; when building a ark of animals take a look at John's answer.
Be carefull that you only take one muskito though....

Cheers, AT
%anonymous% wrote:
Who built the Ark?
Noah, Noah,
Who built the ark?
Brother Noah built the ark.


You have one problem the purpose if the class AnimalContainer. This is something strange where you need only one specimen of each animal species you introduce, without any extensibility. This is counter-productive. To be productive, you need a pair of each species and rename this class to Ark. :-)

This is not just a joke. The purpose if the class and your whole question remains unclear. And it is certainly unrelated to your vague desire to "pass all of the base class information".

If you need something flexible, use the advice by John.

—SA
 
Share this answer
 
See my previous Tips/Tricks. Here is the link:
N-Tier: Begginer's guide in designing their application[^]
This will help you in designing your application :)

Regards,
Eduard
 
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