Re: A test for an Array
- From: "Bill McCarthy" <Bill@xxxxxxxxxx>
- Date: Tue, 20 May 2008 13:11:04 +1000
hi George,
I think you've got some good answers about the need for error handling etc. In VB6 you could do it differently as you could use VarPtr and actually test the structure in memory.
One thing you might want to look at is this part of your code :
<Script language=vbscript>
Function MyFilter(), i
Dim tmpArray()
i = 0
'tmpArray is populated
For Each x in tmpArray
ReDim v(i)
v(i) = x
i = i + 1
Next
Emd Fimction
</Script>
You shouldn't be using ReDim inside a loop, and especially not like that as you are loosing the data that should be in the earlier elements. I'd suggest first dimensioning v to the same bounds as tmpArray, or alternatively using ReDim preserve, but realize that ReDim preserve is inefficient, causing the array to be copied each time.
"George Hester" <hesterloli@xxxxxxxxxxx> wrote in message news:ORBWOBeuIHA.552@xxxxxxxxxxxxxxxxxxxxxxx
I make a dynamic array v something like this:
<Script language=vbscript>
Dim v()
Dim iCnt
iCnt = 0
</Script>
<Script language=vbscript>
Function MyFilter(), i
Dim tmpArray()
i = 0
'tmpArray is populated
For Each x in tmpArray
ReDim v(i)
v(i) = x
i = i + 1
Next
Emd Fimction
</Script>
<Script language=vbscript event=onmousedown for=idT>
Dim vBound, w()
'Problem here
vBound = UBound(v)
If Err.Number = CLng(9)
Err.Clear
Else
If iCnt < vBound Then
'Do stuff
iCnt = iCnt + 1
Else
'Problem here
ReDim v(0)
iCnt = 0
End If
End If
</Script>
There are two problems here and I am hoping someone can show me a better
way. The vBound = UBound(v) will give an error "Subscript out of range" if v
is an "empty" array. So I have checked whether an error has occurred to tell
me that. But I do not like throwing an Error if I can avoid it. Is tthere a
better way to test for an empty array? Not just empty elements because in
fact there are no elements yet. This test is the best I can come up with so
far.
And for the second problem. I do want v to be an empty array most of the
time. Trouble is ReDim v(0) is not an empty array. The first element is
empty but that isn't what I need. I need the array itself to be empty; no
dimensions yet. That is why I have w() Dimed. Because if I could set v = w
then it would be an empty array. But I cna't do that. I get a script error.
It works wuth scaler values but not arrays. So is there some way to make an
array EMPTY again, not just containing empty values? Set v = Empty didn't
work. In other words IsEmpty(v) is still false.
Thanks for any insight here.
--
George Hester
_________________________________
.
- Follow-Ups:
- Re: A test for an Array
- From: George Hester
- Re: A test for an Array
- References:
- A test for an Array
- From: George Hester
- A test for an Array
- Prev by Date: Re: Embed Translucent Image
- Next by Date: Re: HOw to import a text file with fixed column in an access database
- Previous by thread: Re: A test for an Array
- Next by thread: Re: A test for an Array
- Index(es):
Relevant Pages
|