0

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.

2 Answers 2

1
Dim f as Foo
set f = obj.FooArray(0)

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
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem comes from the ambiguity in VB with the FooArray(0) syntax. The parentheses can represent arguments of a Function/Sub call or an array indexer.

The solution is to explicitly declare an array, e.g. fa() as follows:

Dim f As Foo
Dim fa() As Foo
Dim b As New Bar

fa = b.FooArray
Set f = fa(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.