Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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