Convert each record in an ArrayList to an array of strings

Hi,

Is there some method I can use to convert each record in an ArrayList to an array of strings What I'm looking to do is iterate through the ArrayList collection with a For Each loop and convert each record to a 1 dimensional array of strings so I can use the Join method to delimit the record with commas and then write each record back to a text file - I know a longer way to write each record in the ArrayList but it seems like it would be more efficient if I could do what I suggested above. Thanks for any input.

-Dave



Answer this question

Convert each record in an ArrayList to an array of strings

  • JunJie1800

    The above does work if the ArrayList stores a list of strings. I think the problem is that I never fully explained what type of data I'm storing in the ArrayList (sorry about that) - If I was simply storing a list of strings the conversion would be no problem. What I'm storing, though, is instances of the CallRecord class:

    Public Class CallRecord

    Public ID As String

    Public Employee As String

    Public CallReceived As DateTime

    Public Request As TypeOfRequest

    Public Overrides Function ToString() As String

    Dim strArray() As String = {Me.ID, Me.Employee, _

    Me.CallReceived.ToString, CType(Me.Request, Integer).ToString}

    Return (Join(strArray, ","))

    End Function

    End Class

    So each record in the ArrayList contains the 4 fields listed above (which includes a date and an enumerated type as well as strings) and to convert a record to string using DirectCast or CType doesn't work. Anyway, sorry I didn't explain that earlier - I guess I've mainly used ArrayLists to store instances of a class and its fields or properties.

    -Dave


  • Quentin Lafferty

    Dave,

    You implied it and I thought your question maybe more complex than it has been erad here. Then when people didn't notice what you said, I thought I must have read what you said incorrectly.

    Let me think about this for a while.



  • Load

    what I've always done is this.....

    Dim AL As New ArrayList

    AL.Add("Test1")

    AL.Add("Test2")

    AL.Add("Test3")

    AL.Add("Test4")

    Dim a(AL.Count - 1) As String

    For i As Byte = 0 To AL.Count - 1

    a(i) = AL(i)

    Next



  • MaxOrion

    Dave,

    I don't think there's any really super magic way to do this. It think you're going to have to grind it out :(



  • anilF

    Hi,

    The code provided will work, as Visual Basic implicitly does the conversion form Object to String for you.

    Otherwise you can explicitly do the unboxing on every iteration:

    Dim str As String = DirectCast(MyArrayList(i), String)

    'work on str

    Actually if you're using Visual Basic 2005 you might want to consider using System.Collections.Generic.List(Of T), which is basically a strong-typed ArrayList.



  • damienmorton

    This you can used within your for each... iterator on the arraylist.


    Dim s As String() = Regex.Split("Each word in this string will end up being an_

    item in this string array", " ")

    If you run the preceding code s() contains:

    S(0) = "Each"
    S(1) = "word"
    S(2) = "in"
    S(3) = "this"
    S(4) = "string"
    S(5) = "will"
    S(6) = "end"
    S(7) = "up"
    S(8) = "being"
    S(9) = "an"
    S(10) = "item"
    S(11) = "in"
    S(12) = "this"
    S(13) = "string"
    S(14) = "array"
    
    http://www.devx.com/tips/Tip/16279

  • benney

    Hi Ryan,

    Thanks for the suggestion, I had already tried that but just to be sure I wasn't missing something I retried it with both For Each and For Next:

    Dim strCurrent As String

    For i As Integer = 0 To alRecords.Count - 1

    strCurrent = DirectCast(alRecords(i), String)

    MsgBox(strCurrent)

    Next

    It throws an InvalidCastException on the DirectCast line (alRecords is the module-level variable that stores the ArrayList). The file I'm working on is in VS2003 - I'm not too worried about it because I have it working fine with overriding ToString in the class that stores the data for the records of the ArrayList-I just wanted to be sure there wasn't some shortcut I didn't know about.

    Thanks,

    Dave


  • Cherian

    You just want a comma separated list of what fields



  • mnelson

    Hi Renee,

    The 4 fields in the CallRecord class (each record in the ArrayList stores an instance of the CallRecord class with a specific value for each of the 4 fields). The ToString method in the CallRecord class that I posted above returns exactly the comma separated list I want - I was just curious if there was some other way I didn't know about.

    Thanks,

    Dave


  • Mr. Guglev

    Hi Spotty,

    Thanks for the reply.  Unfortunately, as far as I can tell, this method won't work on an ArrayList record because an ArrayList stores each of its elements as System.Object data type.  The ToString method also in this case won't convert the entire ArrayList record to a list of its elements (it instead just returns the fully qualified class name).  Of course I could either use the ToString method on each element of the record or Override ToString in the class that stores the ArrayList data (which is how I've been doing it), but what I'm curious about is if there's some built in method that I don't know about that does something like this for you.  I searched the help files and didn't find anything, but I've been known to miss stuff in there before.

    -Dave


  • Convert each record in an ArrayList to an array of strings