how to test an empty array without getting error?

how to test an empty array without getting error?

Anonymous
Not applicable
482 Views
7 Replies
Message 1 of 8

how to test an empty array without getting error?

Anonymous
Not applicable
...another dumb beginner question...

I have an array which may be empty but when I test it like
If array(0) = empty then
or
if array (0) = nothing then
etc
I get an error like subscript out of range or type mismatch,
so how do I check the contents of an array that may be empty so I can tell if it's safe to pass it to the function I want to use it in?
I've seen a lot of ...On error resume next...or on error go to... but I haven't quite absorbed the process fully, does the program just skip over the error condition and proceed with the next viable statement? I'm used to lisp just bombing and being unable to continue so this seems really weird!
Is that acceptable programming or just a flakey workaround for incorrectly structured code?

Thanks again for any help on this.
Mark
0 Likes
483 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Try something like:

 

if isempty(myarray)=true then

 

endif

 

isnull is also useful.

 

Bud Miller


 


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
...another
dumb beginner question...

I have an array which may be empty but when I test it like
If array(0)
= empty then
or
if array (0) = nothing then
etc
I get an error
like subscript out of range or type mismatch,
so how do I check the
contents of an array that may be empty so I can tell if it's safe to pass it
to the function I want to use it in?
I've seen a lot of ...On error resume
next...or on error go to... but I haven't quite absorbed the process fully,
does the program just skip over the error condition and proceed with the next
viable statement? I'm used to lisp just bombing and being unable to continue
so this seems really weird!
Is that acceptable programming or just a
flakey workaround for incorrectly structured code?

Thanks again for any help on this.
Mark

0 Likes
Message 3 of 8

Anonymous
Not applicable
The On Error statement tells your program what to do next in case of
emergency. In addition to continuing with the next line of code
(Resume Next), you can use GoTo to jump to a labeled section in the
routine. Using Resume Next, all you need do is include an If statement
to handle an expected condition:

On Error Resume Next
tmp = myArray(0)
If Err Then
' a problem occurred so handle it here
End If

--
http://www.acadx.com
"You keep using that word. I do not think it means what you think it
means."

"MarkP" wrote in message
news:f02945a.-1@WebX.maYIadrTaRb...
...another dumb beginner question...
I have an array which may be empty but when I test it like
If array(0) = empty then
or
if array (0) = nothing then
etc
I get an error like subscript out of range or type mismatch,
so how do I check the contents of an array that may be empty so I can
tell if it's safe to pass it to the function I want to use it in?
I've seen a lot of ...On error resume next...or on error go to... but
I haven't quite absorbed the process fully, does the program just skip
over the error condition and proceed with the next viable statement?
I'm used to lisp just bombing and being unable to continue so this
seems really weird!
Is that acceptable programming or just a flakey workaround for
incorrectly structured code?
Thanks again for any help on this.
Mark
0 Likes
Message 4 of 8

Anonymous
Not applicable
Since you, as the progranmmner, are "controlling"
the variable, you should know when it has not been initailized. What is the
context of your problem? If the array has been declared but no elements
assigned, assign/initialize an element:

 

redim  array(0) '** use this assuming it was
decalred as a dynamic array

 

so any future references, ie: UBOUND(array), will
return a value and no "subscript out of range" error occurs.

 

or...

 

use error trapping per Frank's post.

 

 BTW, "nothing" is used to determine if an
object var has been "set" and unless the array was declared as an object type,
you will get a "type mismatch" or "object required" error.

 

-Kirk


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
...another
dumb beginner question...

I have an array which may be empty but when I test it like
If array(0)
= empty then
or
if array (0) = nothing then
etc
I get an error
like subscript out of range or type mismatch,
so how do I check the
contents of an array that may be empty so I can tell if it's safe to pass it
to the function I want to use it in?
I've seen a lot of ...On error resume
next...or on error go to... but I haven't quite absorbed the process fully,
does the program just skip over the error condition and proceed with the next
viable statement? I'm used to lisp just bombing and being unable to continue
so this seems really weird!
Is that acceptable programming or just a
flakey workaround for incorrectly structured code?

Thanks again for any help on this.
Mark

0 Likes
Message 5 of 8

Anonymous
Not applicable
> Since you, as the progranmmner, are "controlling" the
> variable, you should know when it has not been initailized

Unfortunately, that is not always the case. You need look no further
than the GetAttributes method of the AcadBlockReference object for an
example. If called against a block with no editable attributes, the
result is an empty array, not a runtime error. To make matters even
more interesting, IsEmpty and IsNull both return False (even though
there's nothing there), so you're required to implement your own
method for determining whether the array is dimensioned.

> redim array(0) '** use this assuming it was decalred as
> a dynamic array

This is not always possible either. See above. There are several times
when a function will require either a variant or unbounded array as a
parameter.

--
http://www.acadx.com
"You keep using that word. I do not think it means what you think it
means."

"Kirk Thompson" wrote in message
news:5EB9E233217E1D03615DDF4EC3F6EF6C@in.WebX.maYIadrTaRb...
Since you, as the progranmmner, are "controlling" the variable, you
should know when it has not been initailized. What is the context of
your problem? If the array has been declared but no elements assigned,
assign/initialize an element:

redim array(0) '** use this assuming it was decalred as a dynamic
array

so any future references, ie: UBOUND(array), will return a value and
no "subscript out of range" error occurs.

or...

use error trapping per Frank's post.

BTW, "nothing" is used to determine if an object var has been "set"
and unless the array was declared as an object type, you will get a
"type mismatch" or "object required" error.

-Kirk
0 Likes
Message 6 of 8

Anonymous
Not applicable
Thank you so much, all who responded.
Especially Frank for setting me straight on those pesky details. After 5 days of pounding on every variation I could think of I've finally gotten past that dead end.
What a relief. Now to figure out the rest of my errors!
Thanks again
Mark
0 Likes
Message 7 of 8

Anonymous
Not applicable
If you declare your array datatype as Variant then you can use
the IsEmpty function to determine if there is anything in the array. Otherwise
the array is initialized just like an integer variable would be. If you use an
object array variable then use the function IsNothing.

 

Joe


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
...another
dumb beginner question...

I have an array which may be empty but when I test it like
If array(0)
= empty then
or
if array (0) = nothing then
etc
I get an error
like subscript out of range or type mismatch,
so how do I check the
contents of an array that may be empty so I can tell if it's safe to pass it
to the function I want to use it in?
I've seen a lot of ...On error resume
next...or on error go to... but I haven't quite absorbed the process fully,
does the program just skip over the error condition and proceed with the next
viable statement? I'm used to lisp just bombing and being unable to continue
so this seems really weird!
Is that acceptable programming or just a
flakey workaround for incorrectly structured code?

Thanks again for any help on this.
Mark

0 Likes
Message 8 of 8

Anonymous
Not applicable
The IsArray method often prevents a lot of trouble in my projects.
--
Henk
0 Likes