Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a base class "Animal". I have 2 classes inherited from Animal, "Dog" and "Cat". Dog and Cat have identical methods and properties names, but their details are different. During the running of the program I want to select using an OptionBox (or something else) either Dog or Cat. It seems to me that In order to do this, one would have to be able to use a common animal such as "Critter"; to be able to appear to be Dog or Cat be for instantiating the right animal. I'm programming this in C# using Visual Studio Pro 2005. If anyone has an approach to the solution, please post or email to [spam magnet removed].
Thanks, Bob
Posted
Updated 24-Aug-10 8:52am
v3
Comments
Toli Cuturicu 24-Aug-10 14:52pm    
Reason for my vote of 1
homework
Nish Nishant 24-Aug-10 17:17pm    
Reason for my vote of 5
I am not 100% sure this deserves a 1 (that Toli voted), so I am 5-ing it to compensate a little. My rep-score is low for the Q/A forums, so it won't make a big impact here unfortunately.

OK, so you want your Animal to be a member variable, so that it's in scope when you push the Talk button.

C#
public partial class Form1 : Form
{
    private Animal animal;

    private void btnDog_Click(object sender, EventArgs e)
    {
        animal = new Dog();
    }

    private void btnCat_Click(object sender, EventArgs e)
    {
        animal = new Cat();
    }

    private void btnTalk_Click(object sender, EventArgs e)
    {
        if (animal != null)
        animal.Talk();
    }
}


Edit: Added pre tags
 
Share this answer
 
v3
Comments
Christian Graus 24-Aug-10 17:12pm    
*grin* thanks for that. CP does not do anything automatically on my Mac, and I was too lazy to do it myself :(
Nish Nishant 24-Aug-10 17:14pm    
Reason for my vote of 5
Proposed answer
DaveyM69 24-Aug-10 18:15pm    
*laugh* No problem!
I'm not sure I follow this. Your 'Critter' class, is 'Animal'. You would have a collection of animals, which could each be instances of 'dog' or 'cat'.

As someone else said, we don't do homework, not unless the question is very specific and comes with code to show you've tried to do it yourself, given that trying to do it is 100% the point. In this case, I think you've thought about it, which is why I've tried to help, but I admit I don't follow what the issue is, exactly.
 
Share this answer
 
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnDog_Click(object sender, EventArgs e)
    {
        Animal dog = new Dog();
        dog.Talk();  // I don't want the dog to talk here
    }

    private void btnCat_Click(object sender, EventArgs e)
    {
        Animal cat = new Cat();
        cat.Talk();  // Nor do I want the cat to talk here
    }

    private void btnTalk_Click(object sender, EventArgs e)
    {
        // I want the dog or cat to talk here depending on whether i pushed the "Dog" or "Cat" button
    }
}
 
Share this answer
 
v2
Comments
Christian Graus 24-Aug-10 15:49pm    
Please don't push 'answer' to add to your question, edit it, or push 'comment'.
Thanks Cristian,
But in your Answer 3, you did not have a routine that defines what the Cat and Dog talk strings are.
Bob
 
Share this answer
 
Comments
Christian Graus 24-Aug-10 17:12pm    
Bob, I explained to you that the answer button is NOT for you to ask questions. You should push COMMENT for comments like this. The dog and cat classes will overload the Talk method defined in the base class, and will define what the talk string is. The whole purpose of an exercise like this is for you to understand how OO works, surely your textbook explained that ?
DaveyM69 24-Aug-10 17:14pm    
Look at Christian's previous comment!
If you need to respond to an answer, use the "Add Comment" widget below. The Add answer section is for answers not comments! Also, adding an answer won't alert the person you are trying to reach - Add Comment will.
Nish Nishant 24-Aug-10 17:15pm    
He assumes you already have those classes available. Don't you?

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