Solved

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

Posted on 2014-01-30
2
361 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

615 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