Solved

How can I explain structure of a class and methods for below example?

Posted on 2014-01-30
2
339 Views
Last Modified: 2014-02-05
I know this is basic question. But help is highly appreciated.
Let me say I have Class for example ANIMAL. I can have any animal horse,do,cat,lion,monkey and so on.
How can I create a class/classes and its methods to determine which type of animal it is?
0
Comment
Question by:RajG1978
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 39823435
How you go about it will depend on what you want to achieve. If you want each different animal to have its own characteristics and abilities, then you would create a class for each that inherits from the base Animal class.

If on the other hand you don't want/need anything that complicated then you can simply add a property to get/set the type. Something like:
public class Animal
{
    // constructor to set the type when instantiating the object
    public Animal(string type)
    {
         this.Type = type;
    }

    public string Type { get; set; }       // automatic property
}

Open in new window

0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 250 total points
ID: 39826687
For this you can use GetType() method and then call Name property over it.

namespace ConsoleApplication1
{
    class Animal
    {
        public Animal()
        {
        }

        public int Legs { get; set; }
        //Common Methods and properties for Animal
    }

    class Lion : Animal
    {
        //Methods and properties for Lion
    }

    class Cat : Animal
    {
        //Methods and properties for Lion
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Cat();
            string type= animal.GetType().Name;

            animal = new Lion();
            type = animal.GetType().Name;
        }
    }
}

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now