Results 1 to 15 of 15

Thread: problem with arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    problem with arrays

    Hey all
    I have a trouble. In my form, there is a button, which executes such code when clicked on:

    Code:
    If i = 0 Then
    ReDim rarx(0), rary(0), rarz(0) As Long
    ReDim stri(0) As String
    End If
    If i >= 1 Then
    ReDim Preserve rarx(i)
    ReDim Preserve rary(i)
    ReDim Preserve rarz(i)
    ReDim Preserve stri(i)
    End If
    i = i + 1
    And then it gives values to rarx(i), rary(i), rarz(i), stri(i), which are too complex to post here, but are certainly correctly written. After, it has to write me values of all the existing arrays' members using 'Do Until...Loop' and displaying them row by row.

    The question is, when i click on the button for a few times, it only posts the latter ('i') members, whereas all the earlier ones seem to be erased.

    The arrays are not mentioned anywhere else in code, and they are only dimensioned with this very 'ReDim' statement above.

    I'd be very glad if you'd help me out )
    Last edited by kapsz; Mar 10th, 2008 at 04:22 AM.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: problem with arrays

    Where is "i" defined ? If it's not in the Declarations Section of your Form, or a Public Variable in a module, it will be reset to zero each time the button is clicked.

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: problem with arrays

    You are basically erasing anything that was in the arrays with the ReDim Var (0) statements. You may want to move those statements to form load or a different button called "Start Over"

    The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    Where is "i" defined ? If it's not in the Declarations Section of your Form, or a Public Variable in a module, it will be reset to zero each time the button is clicked.
    "i" is defined in the Declarations section, yes

    You are basically erasing anything that was in the arrays with the ReDim Var (0) statements. You may want to move those statements to form load or a different button called "Start Over"

    The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).
    But why is that so? I mean, the ReDim Var (0) statements have to happen only once, in the beginning, and they have exactly the desired effect, am I wrong?

    Anyhow, after I moved them to Form_Load, i get "variable not defined" error on
    Code:
    ReDim Preserve rarx(i)
    after clicking for the first time
    Last edited by kapsz; Mar 10th, 2008 at 04:40 AM. Reason: esthetical unhappiness with the view )

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    btw, as far as i understood, the 'redim' statement can be used instead of 'dim' if arrays are mentioned for the first time and their names are not used anywhere else (e.g. in modules)

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: problem with arrays

    Check / Post the loop where you are assigning the values to the array elements, I suspect there's a logic error there.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    hmm, i belive that perhaps arrays become declared only locally and they exist only for one press of a button. but how to fix it without getting the very same 'variable not defined'?
    Last edited by kapsz; Mar 10th, 2008 at 04:51 AM. Reason: i write badly

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: problem with arrays

    Declare them in the declarations section of the form
    Code:
    Private rarx() As Long
    Private rary() As Long
    Private rarz() As Long
    Private stri() As String
    Edit: Changed the original post as per edgemeal's correction, also changed 'Dim' to 'Private'. Thanks Edgemeal.
    Last edited by Doogle; Mar 10th, 2008 at 04:59 AM.

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: problem with arrays

    Code:
    Option Explicit
    Dim rarx() As Long
    Dim rary() As Long
    Dim rarz() As Long
    Dim stri() As String

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    Quote Originally Posted by Doogle
    Check / Post the loop where you are assigning the values to the array elements, I suspect there's a logic error there.
    No, it's not it as i have tried to place something like

    Code:
    if i>=10 then Text1.Text = stri(10)
    instead of loop

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    Quote Originally Posted by Edgemeal
    Code:
    Option Explicit
    Dim rarx() As Long
    Dim rary() As Long
    Dim rarz() As Long
    Dim stri() As String
    yes, i tried it )
    now i get 'subscript out of range' error when i try to assign the needed value to rarx(i). However, the value itself seems to be perfectly ok and it works fine if assigned to any variable

    EDIT: changing 'dim' to 'private' doesn't result in anything ((
    Last edited by kapsz; Mar 10th, 2008 at 05:09 AM.

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: problem with arrays

    That error means that the value of i is either less than the Lower bound of the array or is greater than the Upper bound of the array.

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: problem with arrays

    Quote Originally Posted by kapsz
    yes, i tried it )
    now i get 'subscript out of range' error when i try to assign the needed value to rarx(i). However, the value itself seems to be perfectly ok and it works fine if assigned to any variable
    You could fix it this way, do the work before you increment the counter.

    Code:
    Option Explicit
    Dim rarx() As Long
    Dim rary() As Long
    Dim rarz() As Long
    Dim stri() As String
    
    Dim i As Integer
    Code:
    Private Sub Command1_Click()
    
    If i = 0 Then
        ReDim rarx(0), rary(0), rarz(0) As Long
        ReDim stri(0) As String
    End If
    
    If i >= 1 Then
        ReDim Preserve rarx(i)
        ReDim Preserve rary(i)
        ReDim Preserve rarz(i)
        ReDim Preserve stri(i)
    End If
    
    ' Add stuff .....
    rarx(i) = 123
    
    i = i + 1 ' Increment counter
    
    For i = 0 To UBound(rarx)
       Debug.Print i & " ="; rarx(i)
    Next i
    
    End Sub

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Location
    Estonia
    Posts
    12

    Re: problem with arrays

    ok, i fixed the problem, thanks guys )
    i just didn't get in the first time that i have to have both 'private rarx() as long' in declarations section and 'redim rarz(0) as long' later

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: problem with arrays

    Quote Originally Posted by kapsz
    ok, i fixed the problem, thanks guys )
    i just didn't get in the first time that i have to have both 'private rarx() as long' in declarations section and 'redim rarz(0) as long' later
    Actually if you already Dim'd them at the module level then you can't change the type anyway, so it really should just be,
    ReDim rarx(0), rary(0), rarz(0)
    ReDim stri(0)

    If you have Help installed in VB type Redim, highlight the word and press F1.

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