Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
Questionhow to get file's details Pin
Ankit Rajpoot27-Nov-08 4:20
Ankit Rajpoot27-Nov-08 4:20 
QuestionPass enum through methods Pin
Deresen27-Nov-08 4:19
Deresen27-Nov-08 4:19 
AnswerRe: Pass enum through methods Pin
Pete O'Hanlon27-Nov-08 4:47
mvePete O'Hanlon27-Nov-08 4:47 
GeneralRe: Pass enum through methods Pin
Deresen27-Nov-08 4:51
Deresen27-Nov-08 4:51 
GeneralRe: Pass enum through methods Pin
Pete O'Hanlon27-Nov-08 5:07
mvePete O'Hanlon27-Nov-08 5:07 
GeneralRe: Pass enum through methods Pin
Deresen27-Nov-08 5:14
Deresen27-Nov-08 5:14 
GeneralRe: Pass enum through methods Pin
PIEBALDconsult27-Nov-08 5:40
mvePIEBALDconsult27-Nov-08 5:40 
QuestionExtracting from a list. Pin
nlowdon27-Nov-08 4:04
nlowdon27-Nov-08 4:04 
Good afternoon all,

I'm learning to work with 'Lists'
I've set up a list of 'zooAnimals' and in the list i've added objects as they have been created.

zooAnimals = new List<Animal>();


newcat1 = new Cat(cat1, catPen, fircat);
newcat2 = new Cat(cat2, catPen, seccat);

newtig1 = new Tiger(tiger1, tigerPen, firtig);
newtig2 = new Tiger(tiger2, tigerPen, sectig);

zooAnimals.Add(newcat1);
zooAnimals.Add(newcat2);

zooAnimals.Add(newtig1);
zooAnimals.Add(newtig2);


I would like to now step through the list to perform certain actions when a button is pressed.
For example - the form has a number of buttons on it - 1 being for individual groups of animals to eat().

I tried with
foreach (Cat thisCat in zooAnimals)
{
    if (thisCat.hunger == 7)
    {
        thisCat.hunger = thisCat.hunger - 5;
    }
}


but this code throws an InvalidCast Exception......
Unable to cast object of type 'Zoo_Animals.Tiger' to type 'Zoo_Animals.Cat'.

Is there a way of stepping through the list to do this ?

Thanks in advance !
Neil
AnswerRe: Extracting from a list. Pin
J4amieC27-Nov-08 4:13
J4amieC27-Nov-08 4:13 
GeneralRe: Extracting from a list. Pin
nlowdon27-Nov-08 4:55
nlowdon27-Nov-08 4:55 
GeneralRe: Extracting from a list. Pin
Malcolm Smart27-Nov-08 6:37
Malcolm Smart27-Nov-08 6:37 
GeneralRe: Extracting from a list. Pin
User 665827-Nov-08 7:07
User 665827-Nov-08 7:07 
QuestionHow to handle the memory Corruption Exception in .Net using C# Pin
abbineni.pavithra27-Nov-08 3:33
abbineni.pavithra27-Nov-08 3:33 
Questionproblem in using VC++ DLL functions in C#.net Pin
kimo_cs27-Nov-08 3:32
kimo_cs27-Nov-08 3:32 
QuestionHow to Send email via gmail smtp server Pin
shovaly27-Nov-08 3:30
shovaly27-Nov-08 3:30 
Questionadding a trackbar into a menustrip? Pin
lane0p227-Nov-08 2:57
lane0p227-Nov-08 2:57 
AnswerRe: adding a trackbar into a menustrip? Pin
Abhijit Jana27-Nov-08 3:22
professionalAbhijit Jana27-Nov-08 3:22 
GeneralRe: adding a trackbar into a menustrip? Pin
lane0p227-Nov-08 3:33
lane0p227-Nov-08 3:33 
GeneralRe: adding a trackbar into a menustrip? Pin
Abhijit Jana27-Nov-08 4:46
professionalAbhijit Jana27-Nov-08 4:46 
QuestionASF Files Pin
dotman127-Nov-08 2:57
dotman127-Nov-08 2:57 
Questioncustomizing crystal reports Pin
harcaype27-Nov-08 2:55
harcaype27-Nov-08 2:55 
AnswerRe: customizing crystal reports Pin
Eddy Vluggen27-Nov-08 4:37
professionalEddy Vluggen27-Nov-08 4:37 
GeneralRe: customizing crystal reports Pin
harcaype28-Nov-08 13:40
harcaype28-Nov-08 13:40 
QuestionMaking a class public Pin
Thekaninos27-Nov-08 2:32
Thekaninos27-Nov-08 2:32 
AnswerRe: Making a class public Pin
Simon P Stevens27-Nov-08 2:53
Simon P Stevens27-Nov-08 2:53 

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.