Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Is there a way to test for the existence of an array (UDT)?

Author
Message
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 21st Apr 2010 19:44
Between DBPro and Matrix1 there are many "exist" commands (bitmap, camera, dll, effect, image, bank, coroutine, lookup, etc.). Is there a way to test for the existence of an array or User-Defined Type short of crashing my program?

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Apr 2010 22:32
When would you not know if an array exists?

Just DIM all the arrays you might need and then check the contents... if the array is empty you know it's not in use.

Really though you can't DIM arrays in a function for a good reason I'm sure.

Fantasy Multimedia
16
Years of Service
User Offline
Joined: 20th Apr 2010
Location: Reno, NV
Posted: 21st Apr 2010 22:48
Quote: "Really though you can't DIM arrays in a function for a good reason I'm sure."


Uhh.. What about lazy constructors?
Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 22nd Apr 2010 02:24
Quote: "Really though you can't DIM arrays in a function for a good reason I'm sure."


You can't create UDTs in functions, but you can DIM arrays just fine since they're global anyway, I do it all the time



<-- Spell based team dueling game!
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 22nd Apr 2010 03:18
I have some complex code that uses many UDTs which are re-used. As this thread bears out, just undimming or re-dimming an existing UDT with strings is not a good idea. One possible fix is to use IanM's clear array command but this will fail if the array does not exist. So an 'array exist' command could be useful here.

We have existence checking commands for everything else, why not arrays?

tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 22nd Apr 2010 06:38
Quote: "We have existence checking commands for everything else, why not arrays?"

Because arrays are not denoted with a number. We don't have 1[0] as an array. So saying "check array exist 1" isn't practical.

Without specific code it is hard to offer suggestions to suit your situation, but the basic idea could be to use global flags that tell you which arrays are DIM'd and which are not.


A 3D marble platformer using Newton physics.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Apr 2010 10:19 Edited at: 22nd Apr 2010 10:20
Quote: "What about lazy constructors?"

What's a lazy constructor? EDIT: Is it the fat builder who did the wall outside my house?

Sorry, my mistake. I was thinking about UDTs, I tend to lump the type command next to my array dim. I'm pretty sure that's what I was thinking of...

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Apr 2010 15:34
use the GET ARRAYPTR function - if it returns a zero then the array either hasn't been DIM'd or has been UNDIM'd

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Apr 2010 16:01
If there's anything @IanM hasn't thought of it ain't worth thinkin' of...

Fantasy Multimedia
16
Years of Service
User Offline
Joined: 20th Apr 2010
Location: Reno, NV
Posted: 22nd Apr 2010 17:55
Quote: "What's a lazy constructor?"
A lazy constructor is something you would use instead of referencing a variable directly, instead you would always reference it using getters and setters, and upon the first 'get', it would initialize the variable, and populate it with whatever value[s] that are necessary.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Apr 2010 18:05
Quote: "A lazy constructor is something you would use instead of referencing a variable directly, instead you would always reference it using getters and setters, and upon the first 'get', it would initialize the variable, and populate it with whatever value[s] that are necessary."

How would you do that in DBP? Just a short example would be great?

I luv learnin' new stuff!

Fantasy Multimedia
16
Years of Service
User Offline
Joined: 20th Apr 2010
Location: Reno, NV
Posted: 22nd Apr 2010 18:11
If someone doesn't chime in with an example before I get home, I will type up an example when I get off work.
basjak
16
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 23rd Apr 2010 16:09
array index valid(array name(0))
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 23rd Apr 2010 23:25
With frightening regularity, Matrix1 saves my butt once again. Gives the proper indication whether the array has been dimmed, redimmed, undimmed, cleared, or emptied.

Basjak, the array index valid returns a zero if the array was cleared first, not a good test.

The help file for get arrayptr seems in error. It reads "Ptr to array = GET ARRAYPTR( Array pointer )". Probably should be array name.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Apr 2010 18:14
Thanks for pointing that out and ruining my aura of infallibility.

Login to post a reply

Server time is: 2026-07-27 20:18:19
Your offset time is: 2026-07-27 20:18:19