Home » excel » c# – Late binding an array

c# – Late binding an array

Posted by: admin March 9, 2020 Leave a comment


I am seriously stumped as I can’t find anything on late binding an array in VBA. Is this even possible? If yes, how? If not – why?

Note: I don’t mind if it’s possible using a native .Net/C# types like

Dim o as Object
set o = CreateObject("System.Array")

Even though the System.Array is COM visible it seems impossible to instantiate it.

Any idea how to late bind an array data type in VBA?

please, don’t mention Dictionary or Collections as this question is quite specific to arrays

Additional info:

This is kind of a follow up on my other question. Since it seems impossible to pass a native VBA array to a native .Net collection without looping I was just wondering whether it’s possible to late bind an array as both as seem safearrays which would mean they are compatible and therefore transfering a VBA array to a .Net one would be possible – please correct me if I am completely wrong.

How to&Answers:
  set o = CreateObject("System.Array")

System.Array is an abstract class. So no, that can’t possibly work. Array types are special, they are normally created by the compiler. The backdoor is Type.MakeArrayType() and you can construct that one.

Nothing you’d want to use. Don’t try so hard, any VBA array is converted to System.Array already. It is just often a non-conformant array, one that doesn’t have its first element at index 0. VBA likes 1, unless you use syntax like Dim arr(0 To 3) As Double or have Option Base 0 in effect. Not always possible, use Array.GetValue() to access a non-conformant array. Review your other question for a sample accessor wrapper.