+ Reply to Thread
Results 1 to 12 of 12

ERASEd Object Array

  1. #1
    Registered User
    Join Date
    10-31-2014
    Location
    NE OH
    MS-Off Ver
    Excel 2007
    Posts
    23

    ERASEd Object Array

    After I Erase an array of objects, how can I test a node to see if still in its erased state? IsEmpty, IsNull and Is Nothing all return False.

    Or is there a better way to clear the array than Erase?

    I'm sure I've seen similar questions asked before but search isn't finding an answer.

    Thanks...
    Excel 2016, Win10

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: ERASEd Object Array

    What is a node?

    When one Erases an array it resets the array to have no elements and no dimensions.

    IsEmpty applies only to cells
    IsNull applies only to values
    Is Nothing applies only to objects. And arrays are not objects.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: ERASEd Object Array

    IsEmpty applies to variant variables.


    The Erase help says this about array of objects.
    Array of objects, Sets each element to the special value Nothing.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    10-31-2014
    Location
    NE OH
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: ERASEd Object Array

    Quote Originally Posted by Andy Pope View Post
    IsEmpty applies to variant variables.


    The Erase help says this about array of objects.
    So then shouldn't this print True?

    Please Login or Register  to view this content.
    Actually I see now that even if I explicitly SET oArray(3) = Nothing, the expression oArray(3) Is Nothing is still false.

    Oh well, I guess I can just use a property to indicate that the element is connected to an object instance. Seems like there should be a better way...

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: ERASEd Object Array

    It seems to be the New keyword.
    I set up a class with a .Name property.
    And ran this test code with these results

    Please Login or Register  to view this content.
    And then ran this test sub with these results

    Please Login or Register  to view this content.
    My guess is that when the array declaration statement includes the New keyword, then oArray(3) is not Nothing, it is an instance of a Class1 object. Hence the False results from the test.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: ERASEd Object Array

    As a follow on, I ran this

    Please Login or Register  to view this content.
    That shows that when one use the New keyword when declaring an array of objects, Erase "passes along" the New.

    The OP quesition is no more puzzling than these results

    Please Login or Register  to view this content.
    Last edited by mikerickson; 08-06-2018 at 02:03 PM.

  7. #7
    Registered User
    Join Date
    10-31-2014
    Location
    NE OH
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: ERASEd Object Array

    Quote Originally Posted by mikerickson View Post
    It seems to be the New keyword.
    I set up a class with a .Name property.
    And ran this test code with these results

    Please Login or Register  to view this content.
    And then ran this test sub with these results

    Please Login or Register  to view this content.
    My guess is that when the array declaration statement includes the New keyword, then oArray(3) is not Nothing, it is an instance of a Class1 object. Hence the False results from the test.
    NEW definitely creates the instance (though not until 1st reference I think) but without NEW (in either the DIM or each SET) there would be no actual instances of the objects, and no reason to erase the array.

    I realize now that my confusion is more basic than arrays and the Erase statement - this simple fragment prints False:

    Please Login or Register  to view this content.
    Edit> Duh! o1 Is Nothing IS a reference that causes re-instantiation. Erase probably does set the elements to Nothing, but there's no way to tell without referencing. That rules out using Class_Initialize to set a IsSomething flag, since looking at the flag would force it to True. I think Set as New should make it workable, and I can also try dim as object, then setting to a "cNull" class (instead of Nothing) so even if the instance persists I can use If TypeOf oVar Is cNull
    Last edited by RangeRover; 08-06-2018 at 08:36 PM. Reason: add

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: ERASEd Object Array

    You can count how many objects have been instansized by putting code like this in a class module

    Please Login or Register  to view this content.
    Then when you run this code, I see that when an array of objects is delcared with the New keyword the elements of that array aren't instansized until the particular element is referenced.

    Please Login or Register  to view this content.
    This confusion is probably why it is considered best practice to not use New in declaration statements, but to explicitly instansize each object. In this case each element of the array.

  9. #9
    Registered User
    Join Date
    10-31-2014
    Location
    NE OH
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: ERASEd Object Array

    Well when I posted this this morning I thought I had figured out enough about objects to ask a semi-intelligent question, but now I think I need to put it on hold until I can spend another couple of hours experimenting. For one thing, I see now that once DIM as NEW is used, ANY reference to the object creates an instance - even a simple Get reference after Set = Nothing recreates an instance. As I get a better understanding of the behavior I think I'll be able to avoid the pitfalls. Thanks for the help...

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: ERASEd Object Array

    That’s why you should never dim as New. See Chip Pearson’s articles on the subject.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: ERASEd Object Array

    Still not sure exactly what you are trying to achieve. Why would you not know if Erase had been used? And why would you care.

    Perhaps rather than an array you could use a collection of objects?

  12. #12
    Registered User
    Join Date
    10-31-2014
    Location
    NE OH
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: ERASEd Object Array

    Quote Originally Posted by Andy Pope View Post
    Still not sure exactly what you are trying to achieve. Why would you not know if Erase had been used? And why would you care.

    Perhaps rather than an array you could use a collection of objects?
    The array contains "min/max" objects in various categories for multiple data sets. I erase it to start each new set, then have to compare each new object to ones already in the array, possibly replacing the array element. If the array element is some equivalent of "nothing" the normal compare won't work - I need to detect and handle that separately.

    A dictionary would work - I could devise a key for the min and max in each category, dict.removeall, then if not dict.exists add it, otherwise compare and replace. Knowing what I know now about object arrays, that does seem cleaner...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel File Erased Own Data
    By highfade in forum Excel General
    Replies: 4
    Last Post: 07-27-2013, 03:47 PM
  2. Avoid data to be erased
    By pansovic in forum Excel General
    Replies: 2
    Last Post: 06-21-2012, 09:34 PM
  3. I need help. Accidentally erased my data....!!!
    By Fotis1991 in forum Excel General
    Replies: 2
    Last Post: 11-11-2011, 04:58 AM
  4. Keeping formulas from being erased
    By Harold Lamb in forum Excel General
    Replies: 2
    Last Post: 01-27-2007, 12:09 AM
  5. [SOLVED] How do I recover an Excel worksheet that I erased?
    By JulianB in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 06-21-2006, 02:40 PM
  6. [SOLVED] Global array data got erased after removing reference in VBA
    By Santh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2006, 09:00 AM
  7. Hello all, I need help with an erased sheet!
    By DTS in forum Excel General
    Replies: 0
    Last Post: 01-03-2006, 12:20 PM
  8. ReDim Object array as parameter of Variant array
    By Peter T in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-10-2005, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1