.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: How To Convert An ArrayList To A String Array


Here’s a quick helper post for those looking to convert an ArrayList into a String array. I ran into this problem because I code in VB.NET and the C# code is different.

In VB:

 
'if you have an ArrayList named data
 Dim str() As String = data.ToArray(GetType(String))

In C#:

//if you have an ArrayList named “data”
string[] strings = (string[])data.ToArray(typeof(string));Hope this helps!

Advertisements

December 19, 2007 - Posted by | ASP.NET, C#, Tips & Tricks, VB.NET

10 Comments »

  1. Thanks for this! You just solved something I was struggling with! I was having this problem and came to this post via google.

    Comment by Keith B. | February 13, 2008 | Reply

  2. Glad I could help 🙂 Thanks for the comment, Keith

    Comment by Some.Net(Guy) | February 13, 2008 | Reply

  3. Thanks for the C# code. After googling and trying a couple sites, I came across to this forum. The code works great!

    Comment by Andy S. | April 2, 2008 | Reply

  4. thank u for the code. it helps me.
    thanks again…..
    nice day everyday

    Comment by deepak | May 10, 2008 | Reply

  5. Hi,
    Its correct.

    Thanks alot.

    Comment by Nageshwararao.c | May 15, 2008 | Reply

  6. Hi,

    Thanx a lot,
    It’s working

    Comment by Pankaj | May 20, 2009 | Reply

  7. I am using option strict on, so when using this code it appear an error saying option strict on does not allow implicit conversion from system array to 1-dimensional array of string. Can you help me how to over come this? Not turn off the option strict on.
    Thanks,

    Comment by Hi, | July 13, 2009 | Reply

    • To avoid the option explict warning you need to cast the result…

      Dim str() As String = DirectCast(data.ToArray(GetType(String), String())

      Comment by Todd | July 7, 2010 | Reply

  8. can anyone please provide the code for
    converting string array into arraylist in single line….
    like above

    Comment by sunil | October 8, 2009 | Reply

  9. I very much appreciated your writings and sharing your thoughts to everyone. I expect more articles to read for the next time.

    Regards,
    Noah Group

    Comment by Melissa | October 26, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: