Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-30
2
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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