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.
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:
...in C#
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!
or
Updating, as this is extremely old. You should, of course, use string.Join now. It didn't exist as an option at the time of writing.
The solutions so far are all quite complicated. The idiomatic solution should doubtless be:
There's no need for fancy acrobatics in new framework versions. Supposing a not-so-modern version, the following would be easiest:
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 theConvert
class is generally not very cleanly designed. I tend to avoid it, especially since it's completely redundant.Something like:
Which basically loops ya know...
EDIT
how about:
string.Join(" ,", myArrayList.ToArray());
This will work with .net framework 4.5Here's a simple example demonstrating the creation of a comma delimited string using String.Join() from a list of Strings: