Cannot access array elements in COM created with C#

Asked 2 years ago, Updated 2 years ago, 120 views

I am making a COM for Excel with C#.

Parameter.cs

 ClassInterface (ClassInterfaceType.None)
ComDefaultInterface (type of (IParameter))]
ComVisible (true)
public class parameter
{
    public Member [ ] Members {get;set;}

    public Parameter()
    {
        Members = new Member [2];
        Members[0] = new Member() {Name="A"};
        Members[1] = new Member() {Name="B"};
        Members[2] = new Member() {Name="C"}
    }
}

IParameter.cs

 InterfaceType (ComInterfaceType.InterfaceIsDual)
ComVisible (true)
public interface IParameter
{
    Member [ ] Members {get;set;}
}

When generating an instance of a member class on the VBA side, it is stated as follows:

Dimparam as Object
Set param=CreateObject("Parameter")←Successful

Dim count as Integer
count=UBound(param.Members)←count contains 2
MsgBox param.Members(0).Name←Error!0th element inaccessible

Although the explanation is longer, the UBound() value is returned correctly depending on the number of elements in UBound(param.Members above, but an error occurs when trying to access each element.
The error message is as follows:

Wrong number of arguments or invalid property assignment

How can I access each element?

c# vba com

2022-09-30 13:59

2 Answers

In VB, UBound is not "number", but "maximum number of subscripts".

In the original C# code, for "new Member[2]" (number of elements 2), there is a code that expects "member[2]=...", but what is the number of elements?

If the number of elements 2 is correct and the LBound is 1, then the subscripts are 2 arrays that can be accessed by 1 and 2.Please check out LBound as well.


2022-09-30 13:59

I haven't tried it, but once I get param.Members in Variant, it might work.

Dim vAs Variant
Dim As Member
v=param.Members
Fori= LBounds(v) To UBounds(v)
    Set m=v(i)
Next


2022-09-30 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.