Results 1 to 3 of 3

Thread: How do I delete a record from an array that has nested elements in it?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    5

    Question 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.
    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.

    Name:  tree.jpg
Views: 316
Size:  16.7 KB
    Last edited by AngryDev; Feb 23rd, 2018 at 04:05 PM. Reason: added more info

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    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.
    Last edited by Elroy; Feb 23rd, 2018 at 08:41 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width