Connecting Tech Pros Worldwide Help | Site Map

Checking status of an array

David P. Jessup
Guest
 
Posts: n/a
#1: Jul 19 '05
Good day folks.

Within an ASP I'm working on I need to check whether an array is empty or
not.

Code:
Dim somearray()
'other code: array might have been populated, maybe not
if somearray() <> "" Then 'this is the line that hangs debugging using VID
'code if array is populated
else
'code if array is empty
end if

My problem is coming up when I debug(Using VID) I get this error:
An exception of type 'Microsoft VBScript runtime error: Subscript out of
range' was not handled.

But when I actually launch the page I don't get any errors. Of course if I
put in On Error Resume Next, and I run in debug mode I don't get any errors
through VID and I still obviously I don't get any errors launching the page.

Thanks for any insight from this ASP Newbie


Manohar Kamath [MVP]
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Checking status of an array


David,

You need to make sure you are not going beyond the array bounds, the error
you are getting is because you are not doing so.

Use the UBound() function that returns the last index within the array's
dimension. So, if the array contains 20 elements, the UBound function will
return 19.

lastIndex = UBound(someArray, 1)

Look up the VB help for more info on the method.


--
Manohar Kamath
Editor, .netBooks
www.dotnetbooks.com


"David P. Jessup" <davidATimntDASHtechDOTcom> wrote in message
news:OnGafUckDHA.3732@tk2msftngp13.phx.gbl...[color=blue]
> Good day folks.
>
> Within an ASP I'm working on I need to check whether an array is empty or
> not.
>
> Code:
> Dim somearray()
> 'other code: array might have been populated, maybe not
> if somearray() <> "" Then 'this is the line that hangs debugging using[/color]
VID[color=blue]
> 'code if array is populated
> else
> 'code if array is empty
> end if
>
> My problem is coming up when I debug(Using VID) I get this error:
> An exception of type 'Microsoft VBScript runtime error: Subscript out of
> range' was not handled.
>
> But when I actually launch the page I don't get any errors. Of course if[/color]
I[color=blue]
> put in On Error Resume Next, and I run in debug mode I don't get any[/color]
errors[color=blue]
> through VID and I still obviously I don't get any errors launching the[/color]
page.[color=blue]
>
> Thanks for any insight from this ASP Newbie
>
>[/color]


WIlliam Morris
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Checking status of an array


VBScript's handling of array bounds checking leaves some to be desired. I
get inconsistent results with IsArray(somearray) depending on the state of
the array - GetRows on an empty recordset is a big culprit. What I've done
is write a function (I call it IsArrayEx, and it handles two dimensional
arrays which are what I work with most of the time) that works this way:

Function IsArrayEx(inArray, intUpperBound)
'--- this doesn't address whether or not there's data, only if the array
can be used
IsArrayEx = false

on error resume next

dim tmpVar
tmpVar = inArray(0, ubound(inArray, intUpperBound))
if err.number = 0 then
'--- the array is useable...
IsArrayEx = true
end if
End Function



"David P. Jessup" <davidATimntDASHtechDOTcom> wrote in message
news:OnGafUckDHA.3732@tk2msftngp13.phx.gbl...[color=blue]
> Good day folks.
>
> Within an ASP I'm working on I need to check whether an array is empty or
> not.
>
> Code:
> Dim somearray()
> 'other code: array might have been populated, maybe not
> if somearray() <> "" Then 'this is the line that hangs debugging using[/color]
VID[color=blue]
> 'code if array is populated
> else
> 'code if array is empty
> end if
>
> My problem is coming up when I debug(Using VID) I get this error:
> An exception of type 'Microsoft VBScript runtime error: Subscript out of
> range' was not handled.
>
> But when I actually launch the page I don't get any errors. Of course if[/color]
I[color=blue]
> put in On Error Resume Next, and I run in debug mode I don't get any[/color]
errors[color=blue]
> through VID and I still obviously I don't get any errors launching the[/color]
page.[color=blue]
>
> Thanks for any insight from this ASP Newbie
>
>[/color]


Bob Barrows
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Checking status of an array


WIlliam Morris wrote:[color=blue]
> VBScript's handling of array bounds checking leaves some to be
> desired. I get inconsistent results with IsArray(somearray)
> depending on the state of the array - GetRows on an empty recordset
> is a big culprit.[/color]

This has never failed me:

'open the recordset, then:
dim ar
if not rs.eof then ar = rs.GetRows
rs.close: set rs=nothing
if isarray(ar) then
'process data
else
'handle no-records event
end if

--
HTH,
Bob Barrows - ASP MVP
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


Closed Thread