如何从 VBA 中查找 .net ArrayList 中的文字数字?
How to lookup a literal number in a .net ArrayList from VBA?
.net 运行时通过 mscorlib.dll 公开给 VBA(和其他 COM 客户端)。理论上,这意味着 VBA 程序员可以使用 .net 类。
我使用 ArrayList 进行排序,并试图调用其他方法,但对于 IndexOf 我遇到了问题。它不会为参数采用文字,它需要一个对象。我想也许一个盒装整数可能有效,但也失败了。
那么如何从 VBA 中查找 ArrayList 中的文字数字?
Sub Test()
Dim obj As Object
Set obj = CreateObject("System.Collections.ArrayList")
obj.Add 4
obj.Add 8
obj.Add 12
obj.Add 16
obj.Add 20
Debug.Assert obj.Count() = 5
Debug.Assert obj.Item(2) = 12
'* try a literal
Debug.Print obj.IndexOf(12) 'ERRORS err.Description=Invalid procedure call or argument
'* try a boxed integer
Dim boxedInteger As mscorlib.Int32
boxedInteger.m_value = 12
Debug.Print obj.IndexOf(boxedInteger) 'ERRORS err.Description=Invalid procedure call or argument
End Sub
添加参数 0
Debug.Print obj.IndexOf(12, 0)
The property .IndexOf
indicates the position (=index number) of an
item in an ArrayList.
The first argument is the value you are looking for, the second one
the position after from which you want to check the existence of the value
上面的link定义是第二个参数的行为是在之后,而实际上是从开始。网站作者正在更新以反映这一点。即符合重载方法IndexOf(Object, Int32),就第二个参数而言:
returns the zero-based index of the first occurrence within the range
of elements in the ArrayList that extends from the specified index to
the last element.
例如,Debug.Print obj.IndexOf(4, 0)
,将正确地 return 0
。
我会对一些涵盖此行为的文档感兴趣,其中显然没有将对象作为第一个参数传递。我还没有找到。
.net 运行时通过 mscorlib.dll 公开给 VBA(和其他 COM 客户端)。理论上,这意味着 VBA 程序员可以使用 .net 类。
我使用 ArrayList 进行排序,并试图调用其他方法,但对于 IndexOf 我遇到了问题。它不会为参数采用文字,它需要一个对象。我想也许一个盒装整数可能有效,但也失败了。
那么如何从 VBA 中查找 ArrayList 中的文字数字?
Sub Test()
Dim obj As Object
Set obj = CreateObject("System.Collections.ArrayList")
obj.Add 4
obj.Add 8
obj.Add 12
obj.Add 16
obj.Add 20
Debug.Assert obj.Count() = 5
Debug.Assert obj.Item(2) = 12
'* try a literal
Debug.Print obj.IndexOf(12) 'ERRORS err.Description=Invalid procedure call or argument
'* try a boxed integer
Dim boxedInteger As mscorlib.Int32
boxedInteger.m_value = 12
Debug.Print obj.IndexOf(boxedInteger) 'ERRORS err.Description=Invalid procedure call or argument
End Sub
添加参数 0
Debug.Print obj.IndexOf(12, 0)
The property
.IndexOf
indicates the position (=index number) of an item in an ArrayList.The first argument is the value you are looking for, the second one the position
afterfrom which you want to check the existence of the value
上面的link定义是第二个参数的行为是在之后,而实际上是从开始。网站作者正在更新以反映这一点。即符合重载方法IndexOf(Object, Int32),就第二个参数而言:
returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.
例如,Debug.Print obj.IndexOf(4, 0)
,将正确地 return 0
。
我会对一些涵盖此行为的文档感兴趣,其中显然没有将对象作为第一个参数传递。我还没有找到。