List items [modified]
-
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
-
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
There is a method 'AddRange' that takes a enumerable such as an array or something like that.
Simon
-
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
-
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
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 theArrayList
. 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/2001modified on Thursday, December 11, 2008 11:37 AM
-
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
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....
-
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
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:
-
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:
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