Question : How to convert Arraylist to comma delimeted string

I would like to get the values out of an Arraylist and put them into a string .

I have tried following two methods and they both dont work

string[] strings = (string[])al.ToArray(typeof(string));  --  Throws me an error (System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type)

Also tried.

 string.Join(delimeter, arrList.ToArray(typeof(string)));  -- Throws me a build error.

is there any other way of doing this?

Answer : How to convert Arraylist to comma delimeted string

Hi pratikshahse,

You could try a function which accepts an array, then loop through the items and builds the string....

     public string ToCommaDelimitedString(Array arr)
     {
         System.Text.StringBuilder s = new System.Text.StringBuilder();
         for (int i = 0; i <= arr.Length - 1; i++) {
             s.Append(arr(i).ToString);
             if (i < arr.Length - 1) {
                 s.Append(",");
            }
         }
         return s.ToString;
     }

Regards,

Wayne
Random Solutions  
 
programming4us programming4us