将数组写入ArrayList的函数

时间:2017-12-19 19:22:28

标签: arrays vba excel-vba arraylist excel

我正在尝试创建一个VBA函数,该函数将数组写入.NET System.Collections.ArrayList并返回它。

到目前为止,我有:

Function arrayToArrayList(inArray As Variant) As ArrayList
    'function to take input array and output as arraylist

    If IsArray(inArray) Then
        Dim result As New ArrayList
        Dim i As Long
        For i = LBound(inArray) To UBound(inArray)
            result.Add inArray(i) 'throws the error
        Next i
    Else
        Err.Raise 5
    End If

    Set arrayToArrayList = result
End Function

用(例如)

调用
Sub testArrayListWriter()
    'tests with variant/string array
    'intend to pass array of custom class objects
    Dim result As ArrayList
    Dim myArray As Variant
    myArray = Split("this,will,be,an,array",",")
    Set result = arrayToArrayList(myArray) 
End Sub

但是我收到了错误

  

变量使用Visual Basic不支持的自动化类型

大概是因为我的数组不是正确的类型(可能是Variant)。然而

Dim v As Variant, o As Variant
v = "test_string"
set o = New testClass
result.Add v 'add a string in variant form
result.Add o 'add an object in variant form

没有错误,所以问题不是直接与Variant类型

相关

这里发生了什么,是否有任何方法可以向ArrayList编写未指定类型的数组,或者我必须定义inArray的类型?

3 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点。如果您没有引用mscorlib.dll,首先是后期绑定。您将看到我已将ArrayList更改为Object以声明函数和返回值(retVal)。 test sub还将结果声明为Object。 retVal和结果都是System.Collections.ArrayList的后期绑定。您还需要将inArray和myArray声明为字符串的动态数组。在您的示例中,Split期望返回一个字符串数组,因此您需要提供声明的动态字符串数组。如果您想要其他对象类型,那么您将这些声明的对象类型传递给您的函数。

Private Function arrayToArrayList(inArray() As String) As Object
    'function to take input array and output as arraylist
    Dim retVal As Object
    Set retVal = CreateObject("System.Collections.ArrayList")

    If IsArray(inArray) Then
        Dim i As Long
        For i = LBound(inArray) To UBound(inArray)
            retVal.Add inArray(i)
        Next i
    Else
        Err.Raise 5
    End If

    Set arrayToArrayList = retVal
End Function

Public Sub testArrayListWriter()
    'tests with variant/string array
    'intend to pass array of custom class objects
    Dim result As Object
    Dim myArray() As String
    Set result = CreateObject("System.Collections.ArrayList")
    myArray = Split("this,will,be,an,array", ",")
    Set result = arrayToArrayList(myArray)
End Sub

第二种方法是通过Tools->Reference菜单项添加对mscorlib.dll的引用。出现对话框时,您必须单击“浏览”。您需要浏览到C:\Windows\Microsoft.NET\Framework,然后在您的计算机上选择包含当前.NET版本的文件夹。在该文件夹中,您会找到mscorlib.dllmscorelib.tlb。突出显示以.TLB文件结尾的文件,单击“打开”按钮,在“工具参考”对话框中单击“确定”。

现在,您可以直接在代码中使用Systems.Collections中的任何类。这称为早期绑定,看起来像这样

Private Function arrayToArrayList(inArray() As String) As ArrayList
    'function to take input array and output as arraylist
    Dim retVal As ArrayList

    If IsArray(inArray) Then
        Dim i As Long
        For i = LBound(inArray) To UBound(inArray)
            retVal.Add inArray(i)
        Next i
    Else
        Err.Raise 5
    End If

    Set arrayToArrayList = retVal
End Function

Public Sub testArrayListWriter()
    'tests with variant/string array
    'intend to pass array of custom class objects
    Dim result As ArrayList
    Dim myArray() As String

    myArray = Split("this,will,be,an,array", ",")
    Set result = arrayToArrayList(myArray)
End Sub

答案 1 :(得分:1)

更改

result.Add inArray(i)

result.Add CVar(inArray(i))

答案 2 :(得分:0)

我认为问题在于将Split函数的返回值赋值给一个尚未在任何地方进行过decalred的变量。

尝试添加:

Dim myArray() as string

testArrayListWriter()程序中。