Solved

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

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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