类型铸造字符串

时间:2014-10-13 22:32:57

标签: arrays vb.net excel

我使用数组列表来存储我的字符串。但是,当我运行该程序时,它会抛出一个错误:从类型'String()'到类型'String'的转换无效。我确信我遗漏了一些简单的东西。我认为ArrayList是一个对象列表,它可以是我被发送到数组的任何东西。例如,Strings in,Strings out。这是我正在使用的代码。

   Dim tempString As New ArrayList()

     For Each s As String In tempString
            Dim sAry As String() = bufString.Split(New Char() {ChrW(2)})
            For i As Int16 = 1 To sAry.Length
                xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
            Next
        Next

我也已经明白ArrayList已经折旧了,我应该使用其他东西吗?

1 个答案:

答案 0 :(得分:0)

在快速进入您的案例之前,请先了解其他好的答案

  

What's Wrong with an ArrayList?

     

.NET: ArrayList vs List

你可以谷歌在这方面了解更多...

对于已知的类型集合,使用ArrayLists是不合适的。 对于您的情况,我建议使用通用List集合(List在内存使用方面更有效) 您可以看到我的以下测试用例:

Sub Main()
    Dim tempList As New List(Of String) 'Consider List Rather ArrayList as you knw your requirement for Strings collection
    tempList.Add("string1string2string3string4") 'for testing purpose I've joined "string1, string2, string3 & string4" with ChrW(2) unicode character

    For Each s As String In tempList
        Dim sAry As String() = s.Split(New Char() {ChrW(2)})
        For i As Int16 = 1 To sAry.Length
            'xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
            Console.WriteLine(sAry(i - 1).ToString())
        Next
    Next
    Console.ReadLine()
End Sub
相关问题