【问题标题】:C# like List<T> in VBAC# 类似于 VBA 中的 List<T>
【发布时间】:2013-10-09 12:51:34
【问题描述】:

我想在 VBA 上创建一个 List&lt;T&gt;,就像在 C# 上创建一样,有什么办法可以做到吗?我在 SO 上在这里寻找有关它的问题,但我找不到任何问题。

【问题讨论】:

  • 我找到了一种在 For Each 循环中使用自定义列表的方法,就像使用集合一样,还可以像使用数组一样获取带有 myList(index) 的项目 - 请参阅更新的答案.在 VB6 中工作,但如果您导出 .cls,在记事本中对其进行编辑并将其重新导入游览 VBA 项目,它应该也能正常工作:)
  • 如果您对对象的链接列表(非强类型)没问题,请使用ArrayList。像这样使用Set MyList = CreateObject("System.Collections.ArrayList")。您还可以利用集合或字典来获取列表功能。有关实施细节,请参阅下面的答案。

标签: c# vba collections


【解决方案1】:

泛型出现在 C# 2.0 中;在 VB6/VBA 中,你得到的最接近的是Collection。允许您使用Add、Remove 和Count,但如果您需要更多功能,则需要使用您自己的类来包装它,例如AddRange、Clear 和Contains。

Collection 接受任何Variant(即你扔给它的任何东西),所以你必须通过验证所添加项目的类型来强制执行&lt;T&gt;。 TypeName() 函数可能对此很有用。


我接受了挑战:)

更新 see original code here

列表.cls

向您的 VB6/VBA 项目添加一个新的类模块。这将定义我们正在实现的List&lt;T&gt; 的功能。正如 [Santosh] 的回答所示,我们在选择要包装的 what 集合结构时受到了一些限制。我们可以使用数组,但作为对象的集合是更好的候选对象,因为我们希望枚举器在 For Each 构造中使用我们的 List。

类型安全

List&lt;T&gt; 的问题是 T 说 这个列表是一个确切类型的列表,并且约束意味着一旦我们确定了 T 的类型,该列表实例就会粘住给它。在 VB6 中,我们可以使用TypeName 来获取一个表示我们正在处理的类型名称的字符串,所以我的方法是让列表 know 成为它在就在添加第一项的那一刻:C# 在 VB6 中以声明方式执行的操作,我们可以将其实现为运行时事物。但这是 VB6,所以我们不要为保持数值类型的类型安全而发疯——我的意思是,我们可以在这里比 VB6 更聪明,归根结底,它不是 C# 代码;该语言对此不是很严格,因此折衷方案可能是只允许对小于列表中第一项的数字类型进行隐式类型转换。

Private Type tList
    Encapsulated As Collection
    ItemTypeName As String
End Type
Private this As tList
Option Explicit

Private Function IsReferenceType() As Boolean
    If this.Encapsulated.Count = 0 Then IsReferenceType = False: Exit Function
    IsReferenceType = IsObject(this.Encapsulated(1))
End Function

Public Property Get NewEnum() As IUnknown
    Attribute NewEnum.VB_Description = "Gets the enumerator from encapsulated collection."
    Attribute NewEnum.VB_UserMemId = -4
    Attribute NewEnum.VB_MemberFlags = "40"

    Set NewEnum = this.Encapsulated.[_NewEnum]
End Property

Private Sub Class_Initialize()
    Set this.Encapsulated = New Collection
End Sub

Private Sub Class_Terminate()
    Set this.Encapsulated = Nothing
End Sub

验证值是否属于适当的类型可以是一个函数的角色,为了方便起见,可以将其设为public,因此可以在实际添加值之前通过客户端代码测试该值是否有效。每次我们初始化 New List 时,this.ItemTypeName 都是该实例的空字符串;剩下的时间我们可能会看到正确的类型,所以我们不要费心检查所有可能性(不是 C#,评估不会在 true 语句之后的第一个 Or 处中断):

Public Function IsTypeSafe(value As Variant) As Boolean

    Dim result As Boolean
    result = this.ItemTypeName = vbNullString Or this.ItemTypeName = TypeName(value)
    If result Then GoTo QuickExit

    result = result _
        Or this.ItemTypeName = "Integer" And StringMatchesAny(TypeName(value), "Byte") _
        Or this.ItemTypeName = "Long" And StringMatchesAny(TypeName(value), "Integer", "Byte") _
        Or this.ItemTypeName = "Single" And StringMatchesAny(TypeName(value), "Long", "Integer", "Byte") _
        Or this.ItemTypeName = "Double" And StringMatchesAny(TypeName(value), "Long", "Integer", "Byte", "Single") _
        Or this.ItemTypeName = "Currency" And StringMatchesAny(TypeName(value), "Long", "Integer", "Byte", "Single", "Double")

QuickExit:
    IsTypeSafe = result
End Function

现在这是一个开始。

所以我们有一个Collection。那买了我们Count、Add、Remove和Item。现在后者很有趣,因为它也是Collection 的默认属性,在C# 中它被称为indexer 属性。在 VB6 中,我们将 Item.VB_UserMemId 属性设置为 0,我们得到一个 默认属性:

Public Property Get Item(ByVal index As Long) As Variant
    Attribute Item.VB_Description = "Gets/sets the item at the specified index."
    Attribute Item.VB_UserMemId = 0

    If IsReferenceType Then
        Set Item = this.Encapsulated(index)
    Else
        Item = this.Encapsulated(index)
    End If
End Property

过程属性

在 VBA 中,IDE 不提供任何编辑方式,但您可以在记事本中编辑代码并将编辑后的 ​​.cls 文件导入您的 VBA 项目。在 VB6 中,您有一个工具菜单来编辑这些:

Attribute NewEnum.VB_UserMemId = -4 告诉 VB 使用这个属性来提供一个枚举器——我们只是将封装的Collection 传递给它,它是一个以下划线开头的隐藏属性(不要在家里尝试这个) !)。 Attribute NewEnum.VB_MemberFlags = "40" 也应该使它成为一个隐藏属性,但我还没有弄清楚为什么 VB 不会接受那个。所以为了调用那个隐藏属性的getter,我们需要用[]方括号括起来,因为在VB6/VBA中标识符不能合法地以下划线开头。

NewEnum.VB_Description 属性的一个好处是,无论您在此处输入什么描述,都会在 对象浏览器 (F2) 中显示为描述/迷你文档为您的代码。

项目访问器/“设置器”

VB6/VBA Collection 不允许直接将值写入其项目。我们可以分配 references,但不能分配 values。我们可以通过为Item 属性提供设置器来实现启用写入的List - 因为我们不知道我们的T 是一个值还是一个引用/对象,我们将同时提供Let 和Set 访问者。由于Collection 不支持这一点,我们将不得不首先删除指定索引处的项目,然后在该位置插入新值。

好消息,RemoveAt 和 Insert 是我们无论如何都必须实现的两个方法,RemoveAt 是免费提供的,因为它的语义与封装的 Collection 的语义相同:

Public Sub RemoveAt(ByVal index As Long)
    this.Encapsulated.Remove index
End Sub

Public Sub RemoveRange(ByVal Index As Long, ByVal valuesCount As Long)
    Dim i As Long
    For i = Index To Index + valuesCount - 1
        RemoveAt Index
    Next
End Sub

我对@9​​87654376@ 的实现感觉它可以变得更好,但它基本上读作“在指定索引之后获取所有内容,制作副本;删除指定索引之后的所有内容;添加指定的值,把剩下的项目加回来”:

Public Sub Insert(ByVal index As Long, ByVal value As Variant)
    Dim i As Long, isObjRef As Boolean
    Dim tmp As New List

    If index > Count Then Err.Raise 9  'index out of range

    For i = index To Count
        tmp.Add Item(i)
    Next

    For i = index To Count
        RemoveAt index
    Next

    Add value
    Append tmp

End Sub

InsertRange 可以采用 ParamArray,因此我们可以提供内联值:

Public Sub InsertRange(ByVal Index As Long, ParamArray values())
    Dim i As Long, isObjRef As Boolean
    Dim tmp As New List

    If Index > Count Then Err.Raise 9  'index out of range

    For i = Index To Count
        tmp.Add Item(i)
    Next

    For i = Index To Count
        RemoveAt Index
    Next

    For i = LBound(values) To UBound(values)
        Add values(i)
    Next
    Append tmp

End Sub

Reverse与排序无关,我们可以马上实现:

Public Sub Reverse()
    Dim i As Long, tmp As New List

    Do Until Count = 0
        tmp.Add Item(Count)
        RemoveAt Count
    Loop

    Append tmp

End Sub

这里我想,因为 VB6 不支持重载。如果有一个方法可以添加另一个列表中的所有项目,那就太好了,所以我称之为Append:

Public Sub Append(ByRef values As List)
    Dim value As Variant, i As Long
    For i = 1 To values.Count
        Add values(i)
    Next
End Sub

Add 是我们的List 不仅仅是一个封装Collection 的地方,它有几个额外的方法:如果它是第一个添加到列表中的项目,我们有一段逻辑要在这里执行 - 不是那个我不关心封装集合中有多少项目,所以如果从列表中删除所有项目,T 的类型仍然受到限制:

Public Sub Add(ByVal value As Variant)
    If this.ItemTypeName = vbNullString Then this.ItemTypeName = TypeName(value)
    If Not IsTypeSafe(value) Then Err.Raise 13, ToString, "Type Mismatch. Expected: '" & this.ItemTypeName & "'; '" & TypeName(value) & "' was supplied." 'Type Mismatch
    this.Encapsulated.Add value
End Sub

Add 失败时引发的错误来源是调用ToString 的结果,该方法返回...类型的名称,包括 T 的类型 - 所以我们可以将其设为List&lt;T&gt; 而不是List(Of T):

Public Function ToString() As String
    ToString = TypeName(Me) & "<" & Coalesce(this.ItemTypeName, "Variant") & ">"
End Function

List&lt;T&gt; 允许一次添加多个项目。起初,我使用参数值数组实现了AddRange,但后来我再次意识到,这不是 C#,并且采用 ParamArray 非常方便:

Public Sub AddRange(ParamArray values())
    Dim value As Variant, i As Long
    For i = LBound(values) To UBound(values)
        Add values(i)
    Next
End Sub

...然后我们找到那些Item setter:

Public Property Let Item(ByVal index As Long, ByVal value As Variant)
    RemoveAt index
    Insert index, value
End Property

Public Property Set Item(ByVal index As Long, ByVal value As Variant)
    RemoveAt index
    Insert index, value
End Property

通过提供值而不是索引来删除项目,需要另一种方法来为我们提供该值的索引,因为我们不仅支持 值类型,还支持 引用类型,这会很有趣,因为现在我们需要一种方法来确定引用类型之间的相等性——我们可以通过比较@得到引用相等性 987654401@,但我们需要的不仅仅是这些——.net 框架教会了我关于IComparable 和IEquatable 的知识。让我们将这两个接口合二为一,称之为IComparable - 是的,您可以在 VB6/VBA 中编写和实现接口。

IComparable.cls

添加一个新的类模块并将其命名为 IComparable - 如果您真的打算将它们用于其他用途,那么您可以将它们放在两个单独的类模块中并调用另一个 IEquatable,但这会让你们两个对于您希望能够使用的所有引用类型,要实现的接口而不是一个接口。

这不是模拟代码,只需要方法签名:

Option Explicit

Public Function CompareTo(other As Variant) As Integer
'Compares this instance with another; returns one of the following values:
'   -1 if [other] is smaller than this instance.
'    1 if [other] is greater than this instance.
'    0 otherwise.
End Function

Public Function Equals(other As Variant) As Boolean
'Compares this instance with another; returns true if the two instances are equal.
End Function

列表.cls

使用 IComparable 接口

鉴于我们已经将IComparable 与CompareTo 和Equals 打包在一起,我们现在可以在列表中找到任何值的索引;我们还可以确定列表是否包含任何指定的值:

Public Function IndexOf(value As Variant) As Long
    Dim i As Long, isRef As Boolean, comparable As IComparable
    isRef = IsReferenceType
    For i = 1 To this.Encapsulated.Count
        If isRef Then
            If TypeOf this.Encapsulated(i) Is IComparable And TypeOf value Is IComparable Then
                Set comparable = this.Encapsulated(i)
                If comparable.Equals(value) Then
                    IndexOf = i
                    Exit Function
                End If
            Else
                'reference type isn't comparable: use reference equality
                If ObjPtr(this.Encapsulated(i)) = ObjPtr(value) Then
                    IndexOf = i
                    Exit Function
                End If
            End If
        Else
            If this.Encapsulated(i) = value Then
                IndexOf = i
                Exit Function
            End If
        End If
    Next
    IndexOf = -1
End Function

Public Function Contains(value As Variant) As Boolean
    Dim v As Variant, isRef As Boolean, comparable As IComparable
    isRef = IsReferenceType
    For Each v In this.Encapsulated
        If isRef Then
            If TypeOf v Is IComparable And TypeOf value Is IComparable Then
                Set comparable = v
                If comparable.Equals(value) Then Contains = True: Exit Function
            Else
                'reference type isn't comparable: use reference equality
                If ObjPtr(v) = ObjPtr(value) Then Contains = True: Exit Function
            End If
        Else
            If v = value Then Contains = True: Exit Function
        End If
    Next
End Function

当我们开始询问Min 和Max 的值可能是什么时,CompareTo 方法就会发挥作用:

Public Function Min() As Variant
    Dim i As Long, isRef As Boolean
    Dim smallest As Variant, isSmaller As Boolean, comparable As IComparable

    isRef = IsReferenceType
    For i = 1 To Count

        If isRef And IsEmpty(smallest) Then
            Set smallest = Item(i)
        ElseIf IsEmpty(smallest) Then
            smallest = Item(i)
        End If

        If TypeOf Item(i) Is IComparable Then
            Set comparable = Item(i)
            isSmaller = comparable.CompareTo(smallest) < 0
        Else
            isSmaller = Item(i) < smallest
        End If

        If isSmaller Then
            If isRef Then
                Set smallest = Item(i)
            Else
                smallest = Item(i)
            End If
        End If
    Next

    If isRef Then
        Set Min = smallest
    Else
        Min = smallest
    End If

End Function

Public Function Max() As Variant
    Dim i As Long, isRef As Boolean
    Dim largest As Variant, isLarger As Boolean, comparable As IComparable

    isRef = IsReferenceType
    For i = 1 To Count

        If isRef And IsEmpty(largest) Then
            Set largest = Item(i)
        ElseIf IsEmpty(largest) Then
            largest = Item(i)
        End If

        If TypeOf Item(i) Is IComparable Then
            Set comparable = Item(i)
            isLarger = comparable.CompareTo(largest) > 0
        Else
            isLarger = Item(i) > largest
        End If

        If isLarger Then
            If isRef Then
                Set largest = Item(i)
            Else
                largest = Item(i)
            End If
        End If
    Next

    If isRef Then
        Set Max = largest
    Else
        Max = largest
    End If

End Function

这两个函数允许非常可读的排序 - 因为这里发生的事情(添加和删除项目),我们将不得不快速失败:

Public Sub Sort()
    If Not IsNumeric(First) And Not this.ItemTypeName = "String" And Not TypeOf First Is IComparer Then Err.Raise 5, ToString, "Invalid operation: Sort() requires a list of numeric or string values, or a list of objects implementing the IComparer interface."
    Dim i As Long, value As Variant, tmp As New List, minValue As Variant, isRef As Boolean

    isRef = IsReferenceType
    Do Until Count = 0

        If isRef Then
            Set minValue = Min
        Else
            minValue = Min
        End If

        tmp.Add minValue
        RemoveAt IndexOf(minValue)
    Loop

    Append tmp

End Sub

Public Sub SortDescending()
    If Not IsNumeric(First) And Not this.ItemTypeName = "String" And Not TypeOf First Is IComparer Then Err.Raise 5, ToString, "Invalid operation: SortDescending() requires a list of numeric or string values, or a list of objects implementing the IComparer interface."
    Dim i As Long, value As Variant, tmp As New List, maxValue As Variant, isRef As Boolean

    isRef = IsReferenceType
    Do Until Count = 0

        If isRef Then
            Set maxValue = Max
        Else
            maxValue = Max
        End If

        tmp.Add maxValue
        RemoveAt IndexOf(maxValue)
    Loop

    Append tmp

End Sub

最后的润色

剩下的只是琐碎的东西:

Public Sub Remove(value As Variant)
    Dim index As Long
    index = IndexOf(value)
    If index <> -1 Then this.Encapsulated.Remove index
End Sub

Public Property Get Count() As Long
    Count = this.Encapsulated.Count
End Property

Public Sub Clear()
    Do Until Count = 0
        this.Encapsulated.Remove 1
    Loop
End Sub

Public Function First() As Variant
    If Count = 0 Then Exit Function
    If IsObject(Item(1)) Then
        Set First = Item(1)
    Else
        First = Item(1)
    End If
End Function

Public Function Last() As Variant
    If Count = 0 Then Exit Function
    If IsObject(Item(Count)) Then
        Set Last = Item(Count)
    Else
        Last = Item(Count)
    End If
End Function

List&lt;T&gt; 的一个有趣之处在于,只需在其上调用 ToArray() 即可将其复制到数组中 - 我们完全可以做到这一点:

Public Function ToArray() As Variant()

    Dim result() As Variant
    ReDim result(1 To Count)

    Dim i As Long
    If Count = 0 Then Exit Function

    If IsReferenceType Then
        For i = 1 To Count
            Set result(i) = this.Encapsulated(i)
        Next
    Else
        For i = 1 To Count
            result(i) = this.Encapsulated(i)
        Next
    End If

    ToArray = result
End Function

就是这样!


我正在使用一些辅助函数,它们在这里 - 它们可能属于某些 StringHelpers 代码模块:

Public Function StringMatchesAny(ByVal string_source As String, find_strings() As Variant) As Boolean

    Dim find As String, i As Integer, found As Boolean

    For i = LBound(find_strings) To UBound(find_strings)

        find = CStr(find_strings(i))
        found = (string_source = find)

        If found Then Exit For
    Next

    StringMatchesAny = found

End Function

Public Function Coalesce(ByVal value As Variant, Optional ByVal value_when_null As Variant = 0) As Variant

    Dim return_value As Variant
    On Error Resume Next 'supress error handling

    If IsNull(value) Or (TypeName(value) = "String" And value = vbNullString) Then
        return_value = value_when_null
    Else
        return_value = value
    End If

    Err.Clear 'clear any errors that might have occurred
    On Error GoTo 0 'reinstate error handling

    Coalesce = return_value

End Function

MyClass.cls

当T 是引用类型/对象时,此实现要求该类实现IComparable 接口,以便可排序并查找值的索引。下面是它的完成方式 - 假设您有一个名为 MyClass 的类,其中包含一个名为 SomeProperty 的数字或 String 属性:

Implements IComparable
Option Explicit

Private Function IComparable_CompareTo(other As Variant) As Integer
    Dim comparable As MyClass
    If Not TypeOf other Is MyClass Then Err.Raise 5

    Set comparable = other
    If comparable Is Nothing Then IComparable_CompareTo = 1: Exit Function

    If Me.SomeProperty < comparable.SomeProperty Then
        IComparable_CompareTo = -1
    ElseIf Me.SomeProperty > comparable.SomeProperty Then
        IComparable_CompareTo = 1
    End If

End Function

Private Function IComparable_Equals(other As Variant) As Boolean
    Dim comparable As MyClass
    If Not TypeOf other Is MyClass Then Err.Raise 5

    Set comparable = other
    IComparable_Equals = comparable.SomeProperty = Me.SomeProperty

End Function

List 可以这样使用:

Dim myList As New List
myList.AddRange 1, 12, 123, 1234, 12345 ', 123456 would blow up because it's a Long
myList.SortDescending

Dim value As Variant
For Each value In myList
   Debug.Print Value
Next

Debug.Print myList.IndexOf(123) 'prints 3
Debug.Print myList.ToString & ".IsTypeSafe(""abc""): " & myList.IsTypeSafe("abc")
    ' prints List<Integer>.IsTypeSafe("abc"): false

【讨论】:

【解决方案2】:

我知道这是一篇旧帖子,但除了已经讨论过的内容之外,我想提一下以下内容...

数组列表

您可以使用ArrayList,它是 VBA 中可用的弱类型(使用对象,而非强类型)链表。下面是一些演示基本用法的示例代码。

Sub ArrayListDemo()
    Dim MyArray(1 To 7) As String
    MyArray(1) = "A"
    MyArray(2) = "B"
    MyArray(3) = "B"
    MyArray(4) = "i"
    MyArray(5) = "x"
    MyArray(6) = "B"
    MyArray(7) = "C"
    Set L1 = ToList(MyArray)
    L1.Insert L1.LastIndexOf("B"), "Zz"
    Set L2 = L1.Clone
    L2.Sort
    L2.Reverse
    L2.Insert 0, "----------------"
    L2.Insert 0, "Sort and Reverse"
    L2.Insert 0, "----------------"
    L1.AddRange L2.Clone
    Set L3 = SnipArray(L1, 9, 3)
    Debug.Print "---- L1 Values ----"
    For Each obj In L1
        Debug.Print obj & " (L1 & L3 = " & L3.Contains(obj) & ")"
    Next
    Debug.Print "---- L3 Values ----"
    For Each obj In L3
        Debug.Print obj
    Next
End Sub
Function ToList(ByVal Arr As Variant) As Object
    Set ToList = CreateObject("System.Collections.ArrayList")
    For Each Elm In Arr
      ToList.Add Elm
    Next Elm
End Function
Function SnipArray(ByVal ArrayList As Object, lower As Integer, length As Integer) As Object
    Set SnipArray = ArrayList.Clone
    lower = lower - 1
    upper = lower + length
    If upper < ArrayList.Count Then
        SnipArray.RemoveRange upper, (ArrayList.Count - upper)
    End If
    If lower > 0 Then
        SnipArray.RemoveRange 0, lower
    End If
End Function

字典

另外,很高兴看到字典被提及。以下是一些关于如何在 VBA 中使用字典并将其用作列表的注意事项:

Sub DictionaryDemo()
    'If you have a reference to "Microsoft Scripting Runtime..."'
    Set D = New Dictionary
    'Else use this if you do not want to bother with adding a reference'
    Set D = CreateObject("Scripting.Dictionary")

    'You can structure a dictionary as a zero based array like this'
    D.Add D.Count, "A"
    Debug.Print D(0)

    Set D = Nothing
End Sub

【讨论】:

  • 找到了这个并准备添加 ArrayLists 作为一个选项,但可惜你打败了我......两年前 ;-) 注意 在野外我遇到过在 Win7 和 Win10 机器上安装 .NET 2.0 和 3.5(现在安装相同)时出现问题。您可能必须在cmd prompt 中输入dism 才能正确安装它,否则您可能会得到一个无法描述的Automation error。
  • @GibralterTop ha,这很有趣......唉,我们都来不及提供一个可观的答案,因为原来的答案是一本怪异的小说,在你到达这里之前需要永远滚动浏览就像,哦,我可以只使用一个 ArrayList ... 看到原来的问题是 4 岁,你仍然遇到它,社区可能会受益于为更新答案而构建的问题,它不会被称为重复?... idk,人们对此有不同的看法。
  • 赞成完整性,但公平地说,OP 是在与 List&lt;T&gt; 等效的 VBA 之后,即基于类型安全索引的集合。 ArrayList 是一个 .net 类型,可以在 System.Object 下工作,因此不是类型安全的,并且会导致值类型的装箱惩罚,以及跨 COM/.net 边界的封送惩罚,并且其重载方法使其相当困难告诉你正在调用哪个重载。我对我的怪诞小说一点都不后悔,除了采取包装Collection 的懒惰路线,而不是使用更高效的数组。干杯!
  • @MathieuGuindon 你有一个很好的答案。那不是我最好的选择。这是一个非常详细的答案,很好,但它确实将其他答案埋在它下面(下一个答案只有在滚动条行程的大约 80% 之后才能找到)。这只是一个想法,但您的答案将是个人页面、博客或 Code project 上的精彩帖子,您可以在此处进行总结并链接到。不经意的读者会很感激您的答案摘要,而链接将提供实施细节....另外,感谢您的支持! :)
  • 另外,您对 ArrayList 的局限性是绝对正确的,很高兴指出这一点,但它仍然是一个链表,并且确实是 VBA 语言中最接近 OP 的现成数据结构在问。当然,您可以根据需要构建自己的链表实现(支持数组、支持集合等),但是语言提供的内容与扩展它的方式之间存在差异。
【解决方案3】:

List&lt;T&gt; 是基于索引的集合,它允许将任何数据类型附加到集合对象,这在 VBA 中是不可能的。

VBA 的基于索引的集合

VBA 的键值对集合

或者,您可以在 C# 中创建一个类库并在 VBA 中使用。参考这个link

【讨论】:

  • COM 可见的 C# 代码不能公开泛型类型,需要无参数构造函数,不支持方法重载并且可以跨 COM 互操作工作。这并没有让我发现它变得更简单。在我看来,List&lt;T&gt; 是一个 object 而 VBA 的数组不是 - 这就是我所说的你得到的最接近的是 Collection,因为它是一个暴露类似方法/功能的对象。但我同意,从语义上讲,数组是最接近的。实际上,像 Dim a() As SomeClass 这样的 VBA 数组就像 List&lt;SomeClass&gt; - 我认为 OP 正在寻找好东西/方法。
  • 实际上a() As SomeClass 会更像SomeClass[] 而不是其他东西...
猜你喜欢
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多