无法访问从VBA中的.net代码返回的对象数组 [英] Can't access array of objects returned from .net code in VBA
问题描述
我在C#中创建了一个COM库,我在Excel中使用VBA。
我的库有一个属性返回一个对象数组,但是当我尝试访问数组的元素时,我得到这个编译错误在VBA:错误的参数数或无效的属性赋值。
I have created a COM library in c# which I am consuming from VBA in Excel. My library has a property which returns an array of objects but when I try to access the elements of the array I get this compile error in VBA: "Wrong number of arguments or invalid property assignment".
// C# property
Foo[] FooArray { get { return _fooArray; } }
' Client VBA code
Dim obj as new Bar
Dim f as Foo
set f = obj.FooArray(0)
我尝试返回一个字符串数组,看到同样的错误。
I tried returning an array of strings and saw the same error.
推荐答案
Dim f as Foo
set f = obj.FooArray(0)
这里有四个错误。 FooArray是一个属性,而不是一个函数。该属性不接受参数。 设置不正确,该属性返回一个数组,而不是对象。 f
的类型是错误的,该属性返回一个数组,而不是单个Foo。这应该更接近:
Four mistakes here. FooArray is a property, not a function. The property doesn't take an argument. Set is incorrect, the property returns an array, not an object. The type for f
is wrong, the property returns an array, not a single Foo. This ought to be closer:
Dim f As Foo()
f = obj.FooArray
这篇关于无法访问从VBA中的.net代码返回的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!