Connecting Tech Pros Worldwide Help | Site Map

Checking status of an array

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:57 AM
David P. Jessup
Guest
 
Posts: n/a
Default Checking status of an array

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



  #2  
Old July 19th, 2005, 07:57 AM
Manohar Kamath [MVP]
Guest
 
Posts: n/a
Default 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]


  #3  
Old July 19th, 2005, 07:57 AM
WIlliam Morris
Guest
 
Posts: n/a
Default 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]


  #4  
Old July 19th, 2005, 07:57 AM
Bob Barrows
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.