Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extracting from a list.

Extracting from a list.

Scheduled Pinned Locked Moved C#
tutorialquestionlearning
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nlowdon
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • N nlowdon

      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

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      If you're using .NET 3.5 you can use

      foreach(Cat thisCat in zooAnimals.OfType<Cat>())
      {
      ...
      }

      otherwise you'll have to check each item

      foreach(Animal animal in zooAnimals)
      {
      if(animal is Cat)
      {
      ...
      }
      }

      N M 2 Replies Last reply
      0
      • J J4amieC

        If you're using .NET 3.5 you can use

        foreach(Cat thisCat in zooAnimals.OfType<Cat>())
        {
        ...
        }

        otherwise you'll have to check each item

        foreach(Animal animal in zooAnimals)
        {
        if(animal is Cat)
        {
        ...
        }
        }

        N Offline
        N Offline
        nlowdon
        wrote on last edited by
        #3

        Thank you very much indeed - worked a treat !

        1 Reply Last reply
        0
        • J J4amieC

          If you're using .NET 3.5 you can use

          foreach(Cat thisCat in zooAnimals.OfType<Cat>())
          {
          ...
          }

          otherwise you'll have to check each item

          foreach(Animal animal in zooAnimals)
          {
          if(animal is Cat)
          {
          ...
          }
          }

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #4

          J4amieC wrote:

          foreach(Cat thisCat in zooAnimals.OfType())

          That's neat - and my trigger to start looking at 3.5 finally...

          Knowledge is hereditary, it will find its way up or down. Luc Pattyn
          and since what every time when i want to add button to this control one add two times posted in C# forum

          L 1 Reply Last reply
          0
          • M Malcolm Smart

            J4amieC wrote:

            foreach(Cat thisCat in zooAnimals.OfType())

            That's neat - and my trigger to start looking at 3.5 finally...

            Knowledge is hereditary, it will find its way up or down. Luc Pattyn
            and since what every time when i want to add button to this control one add two times posted in C# forum

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Yeah, didn't know about the OfType extension too, looks really useful in this situation.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups