0

I'm using .NET Library class in Excel VBA. It works. I can compile and register it by RegAsm. Using the Intercaces in .Net and COM attributes like ([InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]) , generally i see methods and objects in VBA Intelisence and I can use it

But ... I would like to use a complex multi-level object domain. Get it by some method and i have problem with list of child objects as a property of parent object.

My Method in C#

    public object GetParentWithChildList()
    {
        var parent = new Parent
        {
            ParentName = "John",
            Children = new List<object>
            {
                new Child {ChildName = "Tom"},
                new Child {ChildName = "Brian"},
                new Child {ChildName = "Eva"}
            }.ToArray()
        };
        return parent;
    }

    public class Parent
    {
        public string ParentName { get; set; }
        public object[] Children { get; set; }
    }
    public class Child
    {
        public string ChildName { get; set; }
    }

object[] , I think is the best way to return list of objects for VBA/VB6 It works when i have 1 level object. e.g. i would like to return just list of objects. object[] wokrs better than Child[] or List

And in VBA it works only partially:

(Main object calls LibDataAccess)

Sub GetParentWithChildListVbaTest()
Dim qda As LibDataAccess
Dim parent As parent

Set qda = New QgeDataAccess
Set parent = qda.GetParentWithChildList()

Debug.Print (parent.ParentName)   ' Works OK - it returns John

Dim child As child
Set child = parent.Children(0)     ' This line returns error: Wrong number of arguments or invalid property assignment
Debug.Print (parent.Children(0).ChildName)
End Sub

My questions are: How to return an object with a list of subobjects as a property (prepared in .NET for VBA ) ? Do I have the wrong code in C # or in VBA ?

2
  • COM has no concept of generics. You have to create some sort of wrapper. See: stackoverflow.com/questions/1474100/… Commented Jan 24, 2020 at 11:46
  • Yes, You have right. that's why I use object[] Children not IList<Child> Children. but problem was with reading it under vba Commented Jan 24, 2020 at 12:22

1 Answer 1

0

Ok, I have solution. Code in VBA had mistakes not in C#

instead of this:

Dim child As child
Set child = parent.Children(0)  
Debug.Print (parent.Children(0).ChildName)

should be something like this:

Dim child As child
children = parent.Children 
Set child = children(0)
Debug.Print (child.ChildName)
Sign up to request clarification or add additional context in comments.

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.