Results 1 to 7 of 7

Thread: ReDim Array Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    11
    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")


  2. #2
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    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..

  3. #3
    Guest
    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?)

  4. #4
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    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..

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    11
    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>
    ---------------------------------------

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    11

    Talking

    Oh! Yes. MarcelB's code work.

    Thank you MarcelB, monte96!

    ^_^

  7. #7
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    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
  •  



Click Here to Expand Forum to Full Width