tags:

views:

6198

answers:

5

I'm storing an ArrayList of Ids in a processing script that I want to spit out as a comma delimited list for output to the debug log. Is there a way I can get this easily without looping through things?

EDIT: Thanks to Joel for pointing out the List(Of T) that is available in .net 2.0 and above. That makes things TONS easier if you have it available.

+10  A: 

Yes, I'm answering my own question, but I haven't found it here yet and thought this was a rather slick thing:

...in VB.NET:

String.Join(",", CType(TargetArrayList.ToArray(Type.GetType("System.String")), String()))

...in C#

string.Join(",", (string[])TargetArrayList.ToArray(Type.GetType("System.String")))

The only "gotcha" to these is that the ArrayList must have the items stored as Strings if you're using Option Strict to make sure the conversion takes place properly.

EDIT: If you're using .net 2.0 or above, simply create a List(Of String) type object and you can get what you need with. Many thanks to Joel for bringing this up!

String.Join(",", TargetList.ToArray())
Dillie-O
There are other "gotchas". One being that this solution isn't as idiomatic as looping through the list yourself. Second, if ToArray traverses the collection, and Join does too, this takes twice as long as a simple foreach loop.
Bill the Lizard
Of course there's always the possibility of using System.Collections.Specialized.StringCollection.
ICR
If you can consider using a List<string> instead, that has a built-in ToArray() method, and eliminiates type issues. BTW - typeof(string) is preferable to Type.GetType("System.String").
Jon B
+2  A: 

Something like:

String.Join(",", myArrayList.toArray(string.GetType()) );

Which basically loops ya know...

EDIT

how about:

string.Join(",", Array.ConvertAll<object, string>(a.ToArray(), Convert.ToString));
mspmsp
oops.... that's not right... how about:string.Join(",", Array.ConvertAll<object, string>(a.ToArray(), Convert.ToString));
mspmsp
A: 
foo.ToArray().Aggregate((a, b) => (a + "," + b)).ToString()

or

string.Concat(foo.ToArray().Select(a => a += ",").ToArray())
Echostorm
Both solutions have several flaws and are just not good.
Konrad Rudolph
How about elaborating instead of being a dick. They fulfill the requirements.
Echostorm
Wow, I'm really sorry! I completely overlooked the “C#” tag on the question and surmised your answers were meant to be valid VB code. Shame on me. But while we're nitpicking: your second answer appends an extra “,” at the end.
Konrad Rudolph
+1  A: 

Also remember that in .Net 2.0 and later, ArrayLists are EVIL.

Joel Coehoorn
Just for the record, what should 2.0 framework apps be using? I'll admit my ignorance here. Shame on me.
Dillie-O
Use one of the generic collections (System.Collections.Generic). The closest to a straight ArrayList is probably List(Of T). The generic collections are type safe and perform MUCH better.
Joel Coehoorn
Why don't you accept an answer that actually creates a string? This answer was meant to be more of helpful comment beside other real answers.
Joel Coehoorn
This is kind of a weird situation. All of the answers listed below my own had "oopses" or downvotes to them, and I didn't get the Cast option of Konrad's solution to work. A couple of other solutions were duplicates of my solution post, so I had to give credit to the List reference. Suggestions?
Dillie-O
+8  A: 

The solutions so far are all quite complicated. The idiomatic solution should doubtless be:

String.Join(",", x.Cast(Of String)().ToArray())

There's no need for fancy acrobatics in new framework versions. Supposing a not-so-modern version, the following would be easiest:

Console.WriteLine(String.Join(",", CType(x.ToArray(GetType(String)), String())))

mspmsp's second solution is a nice approach as well but it's not working because it misses the AddressOf keyword. Also, Convert.ToString is rather inefficient (lots of unnecessary internal evaluations) and the Convert class is generally not very cleanly designed. I tend to avoid it, especially since it's completely redundant.

Konrad Rudolph
Were you going to contribute something worthwhile or just rant like an old man about new things being bad?
Echostorm
Aren't CType and CStr are essentially calls into the Convert class?
Joel Coehoorn
@Echostorm: where do I say that? @Joel: not at all. Most of the time, they call the `MS.VB.CS.Conversions.ToString` helper function which results in a simple `ToString` call. But even this method I wouldn't call *direct*y (as opposed to through the VB cast operators).
Konrad Rudolph