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. List items [modified]

List items [modified]

Scheduled Pinned Locked Moved C#
question
7 Posts 6 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

    Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

    is there a way of putting these all in the one statement ? Something like,

    zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

    (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

    modified on Thursday, December 11, 2008 8:57 AM

    S B realJSOPR P L 5 Replies Last reply
    0
    • N nlowdon

      Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

      is there a way of putting these all in the one statement ? Something like,

      zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

      (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

      modified on Thursday, December 11, 2008 8:57 AM

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      There is a method 'AddRange' that takes a enumerable such as an array or something like that.

      Simon

      1 Reply Last reply
      0
      • N nlowdon

        Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

        is there a way of putting these all in the one statement ? Something like,

        zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

        (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

        modified on Thursday, December 11, 2008 8:57 AM

        B Offline
        B Offline
        Brij
        wrote on last edited by
        #3

        For adding multiple values at a time you need another collection which contains all the item that you want to add and do it as zooAnimals.AddRange(newarr); newarr can be any collection which is inherited from IEnumerable.

        Cheers!! Brij

        1 Reply Last reply
        0
        • N nlowdon

          Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

          is there a way of putting these all in the one statement ? Something like,

          zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

          (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

          modified on Thursday, December 11, 2008 8:57 AM

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          As others have said, you need to put the objects into an ArrayList, but IMHO, that would be a waste of time if that's the only reason you're creating the ArrayList. At some point, you have to add them one at a time *somewhere*, so why not just do it without an intermediate data structure? Just add them one at a time and be done with it.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          modified on Thursday, December 11, 2008 11:37 AM

          1 Reply Last reply
          0
          • N nlowdon

            Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

            is there a way of putting these all in the one statement ? Something like,

            zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

            (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

            modified on Thursday, December 11, 2008 8:57 AM

            P Offline
            P Offline
            Paul Unsworth
            wrote on last edited by
            #5

            Hi Try this:

            object[] myObjects = new object[] { newcat1, newcat2, newtig1, newtig2 };
            ArrayList myArrayList = new ArrayList(myObjects);

            It should keep things nice and tidy for you...

            oooo, the Jedi's will feel this one....

            1 Reply Last reply
            0
            • N nlowdon

              Afternoon all, I'm trying to tidy up my code a little and wondered if its possible to add a number of object to a list on one line of code. i.e

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

              is there a way of putting these all in the one statement ? Something like,

              zooAnimals.Add(newcat1),(newcat2),(newtig1),(newtig2);

              (I know the above DOESN'T work, it's just there to readers an idea of what i mean) Thanks Neil

              modified on Thursday, December 11, 2008 8:57 AM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              assuming ZooAnimal is the type held by zooAnimals, you can do:

              zooAnimals.AddRange(new ZooAnimal[]{newcat1,newcat2,newtig1,newtig2});

              which creates a temporary array and adds its content to zooAnimals. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              realJSOPR 1 Reply Last reply
              0
              • L Luc Pattyn

                assuming ZooAnimal is the type held by zooAnimals, you can do:

                zooAnimals.AddRange(new ZooAnimal[]{newcat1,newcat2,newtig1,newtig2});

                which creates a temporary array and adds its content to zooAnimals. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                I think if yo use completely random names for things, you could obfuscate it more. :) (Yes, I'm one of those weird Outlaw Programmer moods today. Nobody is safe.) :)

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                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