Results 1 to 10 of 10

Thread: [RESOLVED] String.Join with an arraylist?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] String.Join with an arraylist?

    Okay I have an arraylist and I want to output it as one single string into a text file. The trouble is string.join will not let me use the arraylist as an array, and arraylist.toarray doesnt work either. Does anyone have any suggestions? Do I have to use stringbuilder?

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: String.Join with an arraylist?

    String.Join should work fine.
    VB Code:
    1. Dim MyString As String = String.Join(Nothing, MyArrayList.ToArray())
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: String.Join with an arraylist?

    I dont think that will work, because Arraylist is of type "object", and the join needs a string array....

    I usually just loop all items concatenating them...

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: String.Join with an arraylist?

    Thats exactly what I tried and reported that it does not work..

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String.Join with an arraylist?

    VB Code:
    1. Dim myString As String = String.Join(String.Empty, DirectCast(myArrayList.ToArray(GetType(String)), String()))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: String.Join with an arraylist?

    If you dont want to loop all items, you can use the copyto method to copy to a temp string array, then use string.join on it, like the example below:
    VB Code:
    1. Dim MyList As New ArrayList
    2.         MyList.Add("this")
    3.         MyList.Add("that")
    4.         Dim MyStrings(MyList.Count - 1) As String
    5.         MyList.CopyTo(MyStrings)
    6.         Dim MyString As String = String.Join(",", MyStrings)
    7.         MessageBox.Show(MyString)

    P.S. Ok JM.. I knew there was a better way, and that is what I was trying to do... but unsuccessfully I totally missed the part about specifying the type in the ToArray method...
    Last edited by gigemboy; Mar 20th, 2006 at 06:44 PM.

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: String.Join with an arraylist?

    Thanks, just wondering, is it bad practice to use it like this?
    CType(MySettings.ToArray(GetType(String)), String()))

    I typed it after reading yours, and it works so I'm wondering if it is bad?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String.Join with an arraylist?

    Quote Originally Posted by |2eM!x
    Thanks, just wondering, is it bad practice to use it like this?
    CType(MySettings.ToArray(GetType(String)), String()))

    I typed it after reading yours, and it works so I'm wondering if it is bad?
    It's not bad but DirectCast is more efficient. If you know for sure that the object you're casting IS the type that you're casting as then you should use DirectCast as it does not do any type-checking. If the run-time types are not or may not be the same then you should use CType, as it will do some additional type checking and perfrom a conversion if one is required and exists. This means that in the vast majority of cases you should be using DirectCast.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: String.Join with an arraylist?

    Okay, thanks alot

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [RESOLVED] String.Join with an arraylist?

    I was benchmarking the code for the three different methods given, JM's suggestion, my example with CopyTo, and then just looping all items and concatenating them, just to see what the performance differences were. The fastest seemed to be using the .CopyTo method, although just looping the items and concatenating them wasnt far behind at all, both around 78 milliseconds (although concatenating them sometimes jumped to 90+ milliseconds for some reason on some loops). JM' ssuggestion came in between 109ms and 125ms...

    If anyone wants me to post the benchmark code, Id be glad too. The example tested was a very short list of items in an arraylist... not tested on a large list, however...

    ***EDIT - with 100 items in the list, JM's code edged out copyto (1170ms vs 1230ms), and the slowest, was by far looping and concatenating, at 7300ms...
    Last edited by gigemboy; Mar 20th, 2006 at 10:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width