|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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....
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated.
foreach (string img in Directory.GetFiles(imagesDir, "*.jpg"))
{
imageNames.Add(img);
}
Many thanks
Lorna
|
|
|
|
|
hello
DirectoryInfo di = new DirectoryInfo(Path........);
FileInfo[] rgFiles = di.GetFiles("*.jpg");
foreach (FileInfo fi in rgFiles)
{
// add u r needs
}
|
|
|
|
|
Thanks but is that not the same as what I have and I get the complete path - here is another 2 lines of the code:
ArrayList imageNames = new ArrayList();
string imagesDir = Server.MapPath("~/administration/uploaded/");
foreach (string img in Directory.GetFiles(imagesDir, "*.jpg"))
{
imageNames.Add(img);
}
thanks
Lorna
|
|
|
|
|
|
Hi Lorna
This should help you...
DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
foreach (FileInfo file in dir.GetFiles())
{
Console.WriteLine(file.Name);
}
oooo, the Jedi's will feel this one....
|
|
|
|
|
Hurray it works! Thank you very much. For any other interested parties, I rewrote as:
DirectoryInfo dir = new DirectoryInfo(@Server.MapPath("~/administration/uploaded/"));
foreach (FileInfo file in dir.GetFiles())
{
imageNames.Add(file.Name);
}
Which gave me an ArrayList of filenames only (no path).
|
|
|
|
|
Glad to help.
oooo, the Jedi's will feel this one....
|
|
|
|
|
Hi,
.Net2 VS2005
I have a windows forms client app that connects to a web service. In iis I know the port number can be set for the web service (default is 80 but lets say I change this to 81) - and this is repeated in the app.config file in the client. (so this port number is all to do with the web service)
The app connects to the web service and works fine - but is there another similar setting available for the app?? ie on the client machine can I specify what port the client uses connect outward?
Thanks,
Chas
|
|
|
|
|
Can't you specify the port in the URL of the web service with a colon?
ie:
http://www.MyWebService.com:7560
Insert funny quote here.
|
|
|
|
|
Hi,
Yes that is what im doing...
What I was after is what port does the client pc use locally? I guess what im asking is something like what port does a pc use when it connects to anything on the net - and can this be changed?
Thanks,
Chas
|
|
|
|
|
|
hello Neo Andreson
this is not the place for u r FUN >>>
|
|
|
|
|
TJS4u wrote: for u r FUN
"For you are fun"?
No, he's not fun.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Use notepad to test your keyboard, not codeproject
|
|
|
|
|
hello Neo Andreson
use u r to test u r alphabetical skills
not this Board
modified on Thursday, December 11, 2008 7:41 AM
|
|
|
|
|
TJS4u wrote: use u r to test u r alphabetical skills
It looks like you're still testing your alphabetical skills. Stop using TXTspeak and spell out what you're trying to say. It makes your posts difficult to read.
|
|
|
|
|
what is this?
Cheers!!
Brij
|
|
|
|
|
do something for u
and not disturbing others
no job for u ??? why do waste ur time
let all u r dreams come true
have a greate day dear
|
|
|
|