Wednesday, December 01, 2004

Convert ArrayList to String (C#, VB.NET)

A function (with overloads, for char or string deliminator) for converting an ArrayList to a string (with defined deliminator). When calling with ArrayListToString(MyArrayList), default deliminator is ,.

C#

public string ArrayListToString(System.Collections.ArrayList ar)
{
 return ArrayListToString(ar, ',');
}
public string ArrayListToString(System.Collections.ArrayList ar, char delim)
{
 return ArrayListToString(ar, delim.ToString());
}
public string ArrayListToString(System.Collections.ArrayList ar, string delim)
{
 return string.Join(delim, (string []) ar.ToArray(typeof(string)));
}

VB.NET

Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList) As String
 Return ArrayListToString(ar, ","C)
End Function

Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As Char) As String
 Return ArrayListToString(ar, delim.ToString)
End Function

Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As String) As String
 Return String.Join(delim, CType(ar.ToArray(GetType(String)), String()))
End Function

6 comments:

Francisco said...

Excellent! I love your style. Thank you for sharing.

Xesco from Barcelona. Spain.

Jen ;-) said...

You were soooo..... GREAT!!

Razvan said...

This is really a good way of thinking.

Coffeeyesplease said...

Thanks a million...
Simple and straight to the jugular.

Ram said...

Your Idea is excellent...But why do you need unnecessary overloads?

Caio said...

thank you very much, it works perfectly!