How do I remove the elements from a public array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • p4willi
    New Member
    • Jun 2010
    • 22

    How do I remove the elements from a public array

    I have declared a public array and from several sub forms it builds the array. After I press the Save button on my main form, I wish to reset the array to have nothing in it (i.e all the elements are to be removed) since the operation now starts afresh.

    Can someone help here?

    Thanks
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    You can certainly "initialize " the array by setting all the elements to zero or something along those lines.

    Another possibility is to use the ReDim statement to re-dimension the array to have zero elements (all the existing elements will be lost), and then ReDim again to whatever number of elements you need:

    Code:
    Public Constant N As Integer
    
    N = 15
    
    ReDim arr(0)
    ReDim arr(N)

    On the other hand, if it is a locally declared array, why not just let it go out of scope after the subroutine exits, and then get dimensioned and initialized anew again the next time the sub runs?

    Pat

    Comment

    • p4willi
      New Member
      • Jun 2010
      • 22

      #3
      Originally posted by zepphead80
      You can certainly "initialize " the array by setting all the elements to zero or something along those lines.

      Another possibility is to use the ReDim statement to re-dimension the array to have zero elements (all the existing elements will be lost), and then ReDim again to whatever number of elements you need:

      Code:
      Public Constant N As Integer
      
      N = 15
      
      ReDim arr(0)
      ReDim arr(N)

      On the other hand, if it is a locally declared array, why not just let it go out of scope after the subroutine exits, and then get dimensioned and initialized anew again the next time the sub runs?

      Pat
      Pat, thanks for the suggestion. Will this work for dynamic arrays as well?

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        The ReDim statement (look at the VBA help pages for more information) re-dimensions the array length to whatever you specify for "N"; so yes, issuing the two successive ReDim statements ReDim(0) and ReDim(N) should work for a dynamic array.

        If I had to do this though, I would make it a little more transparent and just do it inside a For loop; for instance, for an array that holds numeric values:

        Code:
        For j = 0 to N
           arr(j) = 0
        Next j

        This is basically what VB does when you initialize an array. If the array is string-valued, then arr(j) = '', and so forth.

        Pat

        Comment

        • vb5prgrmr
          Recognized Expert Contributor
          • Oct 2009
          • 305

          #5
          With a dynamic array you can use redim myarray(0), which will still leave an element in the array, or just simple use Erase MyArray...



          Good Luck

          Comment

          Working...