Results 1 to 12 of 12

Thread: subscript out of range.... what am i doing wrong?

Hybrid View

  1. #1

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    subscript out of range.... what am i doing wrong?

    VB Code:
    1. Option Explicit
    2.  
    3. Dim Numbers() As Integer
    4. Dim Index As Integer
    5.  
    6. Private Sub cmdAddToArray_Click()
    7.  
    8. Dim Number As Integer
    9.  
    10. If Index = 0 Then
    11.  
    12. ReDim Numbers(0)
    13. Else
    14.  
    15. ReDim Preserve Numbers(UBound(Numbers) + 1)
    16.  
    17. End If
    18.  
    19. Index = Index + 1
    20.  
    21. Number = txtNumber.Text
    22. txtNumber.Text = ""
    23. txtNumber.SetFocus
    24.  
    25.  
    26.  
    27. End Sub
    28.  
    29. Private Sub CmdDisplay_Click()
    30. Dim Element As Integer
    31. lstNumbers.Clear
    32. For Element = 0 To Index
    33. lstNumbers.AddItem Numbers(Element)
    34. Next Element
    35.  
    36. End Sub
    37.  
    38. Private Sub cmdFindNumber_Click()
    39. Dim Element As Integer
    40. Dim Found As Boolean
    41. Dim SearchNumber As Integer
    42. Element = 1
    43. Found = False
    44. SearchNumber = txtSearchNumber.Text
    45.  Do While (Found = False) And (Element <= Index)
    46.  
    47. If Numbers(Element) = SearchNumber Then
    48. Found = True
    49. Else
    50. Element = Element + 1
    51. End If
    52. Loop
    53. If Found Then
    54. lblDisplaySearch.Caption = "This number IS in the array"
    55. Else
    56. lblDisplaySearch.Caption = "This number is NOT in the array"
    57. End If
    58.  
    59.  
    60.  
    61. End Sub

    hey guys, i have this code but whenever i click "display", which shows the numbers i entered into the array i get a "subscript out of range" error on the 'lstNumbers.AddItem Numbers(Element)' line.

    can you see whats wrong?

    thanks

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: subscript out of range.... what am i doing wrong?

    Please, indent your code ...

    I presume you have populated the array first before you click Display. I'm not sure what your Index variable is for but you could just use UBound instead.

    VB Code:
    1. Private Sub CmdDisplay_Click()
    2. Dim i As Long
    3.     lstNumbers.Clear
    4.     For i = 0 To UBound(Numbers)
    5.         lstNumbers.AddItem Numbers(i)
    6.     Next i
    7. End Sub

  3. #3

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: subscript out of range.... what am i doing wrong?

    Please, indent your code ...
    lol sorry, will do.

    i have populated the array. i tried your code and it stops the erro messagwe although all the numbers in the display are now zero...

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: subscript out of range.... what am i doing wrong?

    That's because in your population routine, you don't actually assign values to any of the array elements.

    Assigning a value to an element would be like this:
    VB Code:
    1. Numbers(x) = val

    Since you haven't done that, all your numbers are zero.

  5. #5

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: subscript out of range.... what am i doing wrong?

    i was trying to make an unlimited length dynamic array.

    i had this line in it before

    VB Code:
    1. Numbers(Index) = Number

    but i was getting the same subscript out of range error

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: subscript out of range.... what am i doing wrong?

    here is some handy code to test if the array is empty or has data...
    VB Code:
    1. Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
    2.  
    3.  
    4. Public Function ArrayIsFilled(ary() As String) As Boolean
    5.    
    6.     If (SafeArrayGetDim(ary) > 0) Then
    7.         ArrayIsFilled = True
    8.     Else
    9.         ArrayIsFilled = False
    10.     End If
    11.    
    12. End Function

    so you could change it to this:
    VB Code:
    1. If ArrayIsFilled(Numbers) Then
    2.     ReDim Preserve Numbers(UBound(Numbers) + 1)
    3. Else
    4.     ReDim Numbers(0)
    5. End If
    and u dont have to track index anymore
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: subscript out of range.... what am i doing wrong?

    i'm getting a type mismatch error now.

    how would i assign the values i type in to an element?

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: subscript out of range.... what am i doing wrong?

    type into a textbox?

    you need to make sure that its a number and convert to a number
    like:
    if IsNumeric(Text1) then
    number(x) = cint(text)
    end if
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: subscript out of range.... what am i doing wrong?

    lol i'm confused....

    i populate my array by typing numbers into a textbox. i can do that. my problem now is that when i want to display what i have in my array it comes up as zero. from penagates' post i somehow have to assign a value to an element. can you shpow me how to do this? i don't understand what
    numbers(x) = val means.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: subscript out of range.... what am i doing wrong?

    If Numbers is an array with 5 elements (0 through 4) and you want to assign a value to element 2, you would do
    VB Code:
    1. If IsNumber(Text1.Text) Then
    2. Numbers(2) = CInt(Text1.Text)
    3. End If

  11. #11

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: subscript out of range.... what am i doing wrong?

    thanks guys. i think what i'm trying to do is to advanced for my knowledge of arrays. anything i seem to do i get the "subscript out of range error". i'm fed up!

    thanks anyway

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: subscript out of range.... what am i doing wrong?

    here is a whole example:
    VB Code:
    1. Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
    2. Dim Numbers() As Integer
    3.  
    4. Public Function ArrayIsFilled(ary() As Integer) As Boolean
    5.    
    6.     If (SafeArrayGetDim(ary) > 0) Then
    7.         ArrayIsFilled = True
    8.     Else
    9.         ArrayIsFilled = False
    10.     End If
    11.    
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15.     If IsNumeric(text1) Then
    16.         If ArrayIsFilled(Numbers()) Then
    17.             ReDim Preserve Numbers(UBound(Numbers) + 1)
    18.         Else
    19.             ReDim Numbers(0)
    20.         End If
    21.         Numbers(UBound(Numbers)) = CInt(text1)
    22.     End If
    23. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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