我可以在VBA中使用ArrayList而不启用.NET Framework 3.5吗?



我被要求准备禁用.NET Framework 3.5,这似乎是VBA中使用库system.collections(ArrayLists,Queues,SortedList等)所必需的。有没有办法在不启用 3.5 的情况下继续使用此库?或者是否有 VBA 中可用的 ArrayList 的替代方案?

这些数据结构中的大多数已经存在了一段时间(大多数自 .NET 1.1 以来),并且是 COM 可见的,因此可以使用后期绑定来访问它们。Windows机器上需要存在某种.NET的变体,它需要这些库来实现Windows的大部分功能。

在未启用 .NET 3.5 的计算机上尝试此方法,看看是否存在问题。

Option Explicit
Public Sub NETGenericCollections()
Dim arrayList       As Object
Dim iter            As Variant
Dim i               As Long
Set arrayList = CreateObject("System.Collections.ArrayList")
With arrayList
.Add "2"
.Add "3"
.Insert 0, "1"
End With
For Each iter In arrayList
Debug.Print "Array List iterations " & iter
Next
Dim stack As Object
Set stack = CreateObject("System.Collections.Stack")
With stack
.Push "1"
.Push "2"
.Push "3"
End With
Debug.Print "Stack peek " & stack.peek() 'see the item without removal
Debug.Print "Stack pop " & stack.pop()
Debug.Print "Stack pop " & stack.pop()
Dim queue As Object
Set queue = CreateObject("System.Collections.Queue")
With queue
.enqueue "1"
.enqueue "2"
.enqueue "3"
End With
Debug.Print "Queue peek " & queue.peek()
Debug.Print "Queue dequeue " & queue.Dequeue()
Debug.Print "Queue dequeue " & queue.Dequeue()
Dim sortedList As Object
Set sortedList = CreateObject("System.Collections.SortedList")
With sortedList
.Add 3, 3 'Key, Value
.Add 2, 2
.Add 1, 1
.Add 5, 1
End With
For i = 0 To sortedList.Count - 1
Debug.Print "Sorted List iter " & sortedList.getkey(i)
Next
End Sub

延伸阅读

System.Collections 文档 - 检查 ComVisible 装饰器,这将告诉您是否可以在代码中使用此集合。

排序列表 - 一些示例,是VBA见解的好网站。

队列

最新更新