Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
Question'List' help ? Pin
nlowdon21-Nov-08 7:45
nlowdon21-Nov-08 7:45 
AnswerRe: 'List' help ? Pin
Pedram Behroozi21-Nov-08 8:10
Pedram Behroozi21-Nov-08 8:10 
AnswerRe: 'List' help ? Pin
Christian Graus21-Nov-08 8:56
protectorChristian Graus21-Nov-08 8:56 
GeneralRe: 'List' help ? Pin
nlowdon21-Nov-08 9:20
nlowdon21-Nov-08 9:20 
GeneralRe: 'List' help ? Pin
Christian Graus21-Nov-08 10:33
protectorChristian Graus21-Nov-08 10:33 
GeneralRe: 'List' help ? Pin
nlowdon21-Nov-08 12:35
nlowdon21-Nov-08 12:35 
GeneralRe: 'List' help ? Pin
User 665821-Nov-08 13:14
User 665821-Nov-08 13:14 
GeneralRe: 'List' help ? [modified] Pin
cor287921-Nov-08 13:31
cor287921-Nov-08 13:31 
Hi there,

I was looking over your post and I think this is a sample of how to accomplish what you are looking for. Look this over:

using System;
using System.Collections.Generic;

public abstract class ZooAnimal
{
    public abstract string Speak();
}

public class Cat : ZooAnimal
{
    public override string Speak()
    {
        return "Meow";
    }
}

public class Lion : Cat
{
    public override string Speak()
    {
        return "Rowr!";
    }
}

public class Program
{
    public static void Main(params string[] args)
    {
        List<ZooAnimal> zooAnimals = new List<ZooAnimal>();

        for (int i = 0; i < 2; i++)
            zooAnimals.Add(new Cat());

        for (int i = 0; i < 3; i++)
            zooAnimals.Add(new Lion());

        for (int i = 0; i < zooAnimals.Count; i++)
        {
            Lion lion = zooAnimals[i] as Lion;

            if (lion != null)
                Console.WriteLine("The lion says: " + lion.Speak());
            else
            {
                Cat cat = zooAnimals[i] as Cat;
                Console.WriteLine("The cat says: " + cat.Speak());
            }
        }

        Console.Read();
    }
}


See this is all just a matter of inheritance and making sure you instantiate your types correctly. In your code example you are adding two animals to the collection but trying to get out two cats. Remember - all cats are animals but not all animals are cats. To get the code you posted to work correctly, you would need to add cats to the zooAnimals collection instead of just animals. This works just fine in C# by the way - an object declared as a base type can always hold an instance of one of its derived types. To illustrate I created a three tiered inheritance example. Notice that in the above example you have ZooAnimals, Cats, and Lions. ZooAnimals is an abstract type, it exists only to serve as a base class for anything that I might consider a zoo animal. Then you have the Cat class. This is also a pretty general class - there are many types of cats, so I derived another class from Cat called Lion. Notice the hierarchy - All Lions are Cats but not all Cats are Lions (illustrated in the final for loop in the static method Main). All Cats are ZooAnimals (as far as this program is concerned at least) all ZooAnimals are not necessarily Cats. It must also follow then that since all Lions are Cats, and all Cats are ZooAnimals, then all Lions are ZooAnimals as well. Hope that helps!

"We are men of action; lies do not become us."

modified on Friday, November 21, 2008 11:39 PM

GeneralRe: 'List' help ? Pin
Christian Graus21-Nov-08 20:54
protectorChristian Graus21-Nov-08 20:54 
QuestionDefining form client area Pin
Chris Copeland21-Nov-08 5:12
mveChris Copeland21-Nov-08 5:12 
AnswerRe: Defining form client area Pin
Rob Smiley21-Nov-08 6:03
Rob Smiley21-Nov-08 6:03 
GeneralRe: Defining form client area Pin
Chris Copeland21-Nov-08 6:17
mveChris Copeland21-Nov-08 6:17 
Questionthe efficieny consideration Pin
Seraph_summer21-Nov-08 3:28
Seraph_summer21-Nov-08 3:28 
AnswerRe: the efficieny consideration Pin
Ennis Ray Lynch, Jr.21-Nov-08 3:34
Ennis Ray Lynch, Jr.21-Nov-08 3:34 
QuestionForm Paint drawing incorrectly Pin
Chris Copeland21-Nov-08 3:14
mveChris Copeland21-Nov-08 3:14 
AnswerRe: Form Paint drawing incorrectly Pin
Ennis Ray Lynch, Jr.21-Nov-08 3:35
Ennis Ray Lynch, Jr.21-Nov-08 3:35 
GeneralRe: Form Paint drawing incorrectly Pin
Chris Copeland21-Nov-08 3:49
mveChris Copeland21-Nov-08 3:49 
GeneralRe: Form Paint drawing incorrectly Pin
Ennis Ray Lynch, Jr.21-Nov-08 3:57
Ennis Ray Lynch, Jr.21-Nov-08 3:57 
GeneralRe: Form Paint drawing incorrectly Pin
Chris Copeland21-Nov-08 4:07
mveChris Copeland21-Nov-08 4:07 
GeneralRe: Form Paint drawing incorrectly Pin
Guffa21-Nov-08 5:01
Guffa21-Nov-08 5:01 
QuestionGeneric Class with Inheritance Pin
MatthysDT21-Nov-08 2:50
MatthysDT21-Nov-08 2:50 
AnswerRe: Generic Class with Inheritance Pin
cor287921-Nov-08 13:41
cor287921-Nov-08 13:41 
AnswerRe: Generic Class with Inheritance Pin
N a v a n e e t h21-Nov-08 17:33
N a v a n e e t h21-Nov-08 17:33 
GeneralRe: Generic Class with Inheritance Pin
MatthysDT23-Nov-08 19:23
MatthysDT23-Nov-08 19:23 
GeneralRe: Generic Class with Inheritance Pin
cor287924-Nov-08 4:05
cor287924-Nov-08 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.