Avatar of pratikshahse
pratikshahse

asked on 

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?
.NET Programming

Avatar of undefined
Last Comment
Wayne Taylor (webtubbs)
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about this:
http://blog.troyd.net/NET+Code+To+Convert+An+ArrayList+To+A+Comma+Delimited+String+CSV+Or+Other+Character+Delimited+String.aspx
Public Shared Function ArrayListToDelimString(ByVal arrayList As ArrayList, ByVal separator As String) As String 
    Dim stringArrayList As New ArrayList 
    For Each o As Object In arrayList 
      stringArrayList.Add(o.ToString()) 
    Next 
    Return String.Join(separator, stringArrayList.ToArray(GetType(String))) 
  End Function

Open in new window

Avatar of pratikshahse
pratikshahse

ASKER

This are the two errors that I get

Error      9      The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments


Error      10      Argument '2': cannot convert from 'System.Array' to 'string[]'      
      
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SwatiBhaskar
SwatiBhaskar


The values in arraylist should be string data type, then only you can convert it to string array.
     
      ArrayList ar = new ArrayList();
      ar.Add("1");
      ar.Add("2");
      ar.Add("3");
      ar.Add("4");

      string[] str = (string[]) ar.ToArray(typeof(string));
   
      return string.Join(",", str ).ToString();
>>The values in arraylist should be string data type, then only you can convert it to string array.

That's not entirely correct. They can be any type, as long as they are converted to a string type to be added to the comma delimited string. Obviously this is what was causing the error in the first place, and the asker's question was how to avoid that. For example, what if one item in the arraylist was of an integer type?? The "ToArray" code will fail. Both angelIII's and my code eliminates this problem.

Wayne
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo