1 Attachment(s)
How do I delete a record from an array that has nested elements in it?
With the sub i have below...
Code:
Public Sub DeleteArrayItem(arr As Variant, index As Long)
Dim i As Long
For i = index To UBound(arr) - 1
arr(i) = arr(i + 1)
Next
arr(UBound(arr)) = Empty
End Sub
I can successfully remove a record from a simple array. But when the array gets complicated, I get the following error.
Quote:
Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions
Here is a pic of the array from a watch windows. Lets say I want to delete a3Tree(1) for example.
Attachment 156663
Re: How do I delete a record from an array that has nested elements in it?
Hi AngryDev,
I would think the error you're getting is precisely what it says. Basically, a UDT-type-variable can't be assigned to a Variant-type-variable (without jumping through some hoops).
The title of your thread is "How do I delete a record from an array that has nested elements in it?". However, I'm not at all clear on how that relates to your problem. When you "Erase" a dynamic array, all nested-arrays are automatically erased. Furthermore, if the array has objects in it, all of those objects are automatically uninstantiated. If the array happens to have UDTs in it, they're erased too.
Now, you can't mix Variants and UDTs, even through nesting. I feel like that may be what you're doing. No matter how deeply things are nested, if you have a Variant array, nowhere downstream (nests) can you have a UDT (well, without jumping through those hoops).
There are two ways to get a UDT into a Variant (array or not) that I know of. (I suppose a third is to use some CopyMemory hack, but I'll ignore that one.) The two ways are: The first: set up a TypeLib (.TLB type file) that defines your UDT (rather than defining it in your program with "Type SomeVarName"). But that takes knowledge of how to build TypeLibs. And the second: wrap the UDT inside of a .CLS object. If a UDT is all wrapped into a Class (.CLS), you can assign an object created with that Class to a Variant, even if the Class has a UDT wrapped into it.
Maybe something in there will help you.
Good Luck,
Elroy
EDIT1: Actually, after staring at your post some more, I think I see the problem. It's addressed in my comments above, but not specifically. Your problem is that your "Public Sub DeleteArrayItem(arr As Variant, index As Long)" line of code declares your array (arr) as a Variant. And, I'm guessing that you're trying to send in some UDT array (or an array that has UDTs nested somewhere in it. That's your problem.
EDIT2: Sorry, but just to be clear. Variants and UDTs don't mix well.
Re: How do I delete a record from an array that has nested elements in it?
Variants can hold UDTs just fine... as long as the UDT is wrapped as an object. That's what your typelib is all about.
User-Defined Data Types