How to test for empty array of a TYPEd variable?

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16801
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

How to test for empty array of a TYPEd variable?

Post by ChrisGreaves »

Empty4x4.jpg
I use arrays of TYPE to build structures that are useful to my logic.
In the PrtScr capture above the VBE seems (to me) to object to my test to see if, indeed, the structure being passed as an argument has any content.
Since this is a general utility function, I feel that I'd rather not depend on a programmer remembering to prime the srray.

I tried ISNULL and ISEMPTY but get the same error message, which SEEMS TO ME that:-
(1) I can't use TYPE/END TYPE to define a custom structure and
(2) THEN pass an array of that structure as an argument and
(3) THEN test to see if the array of structure has been initialized.

I'm fairly certain that ISMISSING is not wanted here because the parameter is NOT missing, it is supplied.

Testing as "If UBound(stk) > 0" gives me a "subscript is out of range", which suggests I am about to be taken down the rabbit-hole of ON ERROR

Thanks in advance for any suggestions.
Cheers, Chris (attached Word2003/VBA module)
You do not have the required permissions to view the files attached to this post.
Fill your face with laughter, then there will be no room for tears.

User avatar
HansV
Administrator
Posts: 80088
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to test for empty array of a TYPEd variable?

Post by HansV »

Why do you want to pass an array of TYPEStack to the function tblstkPop if you want to return a single Table object?
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16801
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: How to test for empty array of a TYPEd variable?

Post by ChrisGreaves »

HansV wrote:
23 Dec 2024, 21:06
Why do you want to pass an array of TYPEStack to the function tblstkPop if you want to return a single Table object?
Good question! The correct answer is probably "because I got stymied at this first piece of code and hadn't fleshed out the rest of the code.

The structure TYPEStack has an element that right now is a simple Table; but that Table will be replaced by a home-grown structure of a table; today I will be content to push/pop tables.

My TYPE for the stack needs to hold at least a descriptor of the table, so, Real Soon Now, a structure for tables. The stack itself has a structure -the table being called, and the return address (row) in the table that is doing the calling.

When I exit a subroutine/table that should make me RETURN to the calling table.
To that end, when I POP the stack I want to receive a table (well, yes, a table structure, but for now it will be enough to show that I can return the table itself.

I have used TYPE/END TYPE before, but I can't recall testing the validity of a structure of a TYPE before. I suspect that all my previous arrays of TYPE structures were developed in the code and were not subject to errors from outside data.
That is, this is the first time I've tried to test validity of an array of "user-defined types"

Cheers, Chris
Fill your face with laughter, then there will be no room for tears.

User avatar
HansV
Administrator
Posts: 80088
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to test for empty array of a TYPEd variable?

Post by HansV »

I'm afraid that's too complicated for me. My brain hurts...
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16801
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: How to test for empty array of a TYPEd variable?

Post by ChrisGreaves »

HansV wrote:
23 Dec 2024, 22:06
I'm afraid that's too complicated for me. My brain hurts...
Mine too, and I have wasted my time and yours.
I started off with a program that interpreted data within tables in a document, and am now switching to a document-management system that opens documents and makes their contents available to program code that interprets data within tables in a document. Interpreting the data involves opening more documents ...

I think this is akin to pulling a sock inside out - plunge the arm into the sock, grab the toe, then pull the sock off the wrist.

This sort of confusion should tell me that I haven't thought the process through, so it's back to paper and pencil for me.

I would not be upset if this thread were deleted.

I do believe in TYPE structures and will continue to use them. My mistake lies in using the structures defined ten years ago to do a job for which they were not intended.

Thanks, Chris
Fill your face with laughter, then there will be no room for tears.

User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: How to test for empty array of a TYPEd variable?

Post by SpeakEasy »

Try using a class instead of Type ...

Oh, and IsEmpty doesn't really work for arrays.

Try a cheat:

If (Not stk) = True Then MsgBox "Uninitialised dynamic array"

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16801
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: How to test for empty array of a TYPEd variable?

Post by ChrisGreaves »

SpeakEasy wrote:
24 Dec 2024, 14:50
Try using a class instead of Type ...
Thank you SpeakEasy. It's been so long ... I'd forgotten about CLASSes.
They might be overkill for me here. I recall that I can have properties and methods with class objects; is that correct?
If so then some/most of the procedure code I have written this day would become methods if I went to class objects. PUSH and POP being obvious examples.

The simplicity of dear old TYPE structures appeals to me; I can create data structures that make sense, and anyone who downloads the VBA code can get to work quickly without having to learn something complicated (like CLASS).
Indeed, now I think about it, these TYPE structures are akin the the definitions of different record layouts we provided in our COBOL Data Divisions.
Oh, and IsEmpty doesn't really work for arrays. Try a cheat: If (Not stk) = True Then MsgBox "Uninitialised dynamic array"
This looks promising. I plan to try it out first thing tomorrow morning. It will be my best Christmas present, if only because it is the only one to date :sniff: :xsad:
Thanks again!
Cheers, Chris
Fill your face with laughter, then there will be no room for tears.

User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: How to test for empty array of a TYPEd variable?

Post by SpeakEasy »

>They might be overkill for me here

Don't overthink (or overcomplicate it). You don't have to go to a full class solution, just use a very, very simple class instead of a UDT. Your main code remains unchanged (well, except the change from IsEmpty :grin: .. er, and changing Null to Nothing) ...

Oh, and I used Collection instead of a Table simply because I didn't have a Table object to hand, not because I am suggesting it as an alternative
classinsteadoftype.png
And finally - Merry Christmas ...
You do not have the required permissions to view the files attached to this post.