|
-
Oct 12th, 2000, 08:44 PM
#1
Thread Starter
New Member
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")
-
Oct 12th, 2000, 11:34 PM
#2
Frenzied Member
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:
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(lngUBound)
newarray(lngUBound) = strData
End If
End Sub
oOOo--oOOo
__ /\/\onte96
oOOo--oOOo
Senior Programmer/Analyst
MCP
[email protected]
[email protected]
Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..
-
Oct 13th, 2000, 06:47 AM
#3
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?)
-
Oct 13th, 2000, 09:53 AM
#4
Frenzied Member
Actually.. I noticed an error in my code:
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
%>
oOOo--oOOo
__ /\/\onte96
oOOo--oOOo
Senior Programmer/Analyst
MCP
[email protected]
[email protected]
Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..
-
Oct 13th, 2000, 08:20 PM
#5
Thread Starter
New Member
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>
---------------------------------------
-
Oct 13th, 2000, 08:24 PM
#6
Thread Starter
New Member
Oh! Yes. MarcelB's code work.
Thank you MarcelB, monte96!
^_^
-
Oct 13th, 2000, 10:59 PM
#7
Frenzied Member
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...
oOOo--oOOo
__ /\/\onte96
oOOo--oOOo
Senior Programmer/Analyst
MCP
[email protected]
[email protected]
Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|