Click to See Complete Forum and Search --> : ReDim Array Problem
itjacky
Oct 12th, 2000, 08:44 PM
When I run the following VBScript, the error said : The array index over range of 'lngUBound' in line 4.
Can anyone help me ?
1. Dim newarray()
2. Sub add_array(name)
3. Dim lngUBound
4. lngUBound = UBound(newarray) + 1
5. ReDim Preserve newarray(lngUBound)
6. newarray(lngUBound) = name
7. End Sub
8. add_array("abc")
9. add_array("def")
10. add_array("hello")
monte96
Oct 12th, 2000, 11:34 PM
Your array when first initialized, has no UBound. You will need to test if it has been initialized yet. You can test to see if UBound IsNumeric- make sure that on error resume next is on otherwise it will still cause an error:
<%
Dim newArray()
'No parenthesis here- its a sub not a function
add_array "abc"
add_array "def"
add_array "hello"
Sub add_array(strData)
Dim lngUBound
on error resume next
'The error here will cause this to be false
If IsNumeric(UBound(newArray)) Then
ReDim Preserve newarray(lngUBound)
newarray(lngUBound) = strData
End If
End Sub
To also add the value to the array if the array had not been initialised yet, you could change the sub to this:
Sub add_array(strData)
on error resume next
ReDim Preserve newarray(UBound(newArray) + 1)
if Err.Number <> 0 then
Err.Clear
Redim newArray(1)
End if
newarray(UBound(newArray)) = strData
End Sub
(Okay, for some reason it keeps f%^$ing up my indentations.. anyone know why?)
monte96
Oct 13th, 2000, 09:53 AM
Actually.. I noticed an error in my code:
<%
Dim newArray()
'No parenthesis here- its a sub not a function
add_array "abc"
add_array "def"
add_array "hello"
Sub add_array(strData)
Dim lngUBound
on error resume next
'The error here will cause this to be false
If IsNumeric(UBound(newArray)) Then
ReDim Preserve newarray(UBound(newArray) + 1)
newarray(lngUBound) = strData
Else
ReDim newArray(0)
newarray(lngUBound) = strData
End If
End Sub
%>
itjacky
Oct 13th, 2000, 08:20 PM
However, when I apply the function in HTML page. It seems that no value added into "add_array".
The error message said : newArray(0) excess the range.
-----------------------------------
<html>
<head>
<script language = "VBScript">
Dim newArray()
'No parenthesis here- its a sub not a function
add_array "abc"
add_array "def"
add_array "hello"
Sub add_array(strData)
Dim lngUBound
on error resume next
'The error here will cause this to be false
If IsNumeric(UBound(newArray)) Then
ReDim Preserve newarray(UBound(newArray) + 1)
newarray(lngUBound) = strData
Else
ReDim newArray(0)
newarray(lngUBound) = strData
End If
End Sub
</script>
<body>
<script language = "VBScript">
document.write newArray(0)
document.write newArray(1)
document.write newArray(2)
</script>
</body>
</html>
---------------------------------------
itjacky
Oct 13th, 2000, 08:24 PM
Oh! Yes. MarcelB's code work.
Thank you MarcelB, monte96!
^_^
monte96
Oct 13th, 2000, 10:59 PM
Yea.. big duh here for me.. in my code there lngUBound is never set to anything (And variables in VBScript do not initialize to 0 since they are variants)
glad ya got it working tho...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.