Results 1 to 14 of 14

Thread: [RESOLVED] Array Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Resolved [RESOLVED] Array Help

    Here's an example of what i try to archive
    VB Code:
    1. Public arrTemp() as String
    2.  
    3. Private Sub Command1_Click()
    4. 'Cannot Redim Preserve here since i don't know the size
    5. For i = 0 to Ubound(arrTemp)
    6. 'code to compare array
    7. If  condition = arrTemp(i) then exit sub
    8. Next
    9.  
    10. ReDim Preserve arrTemp(UBound(arrTemp) + 1)
    11. arrTemp(UBound(arrTemp)) = "abc [" & "]"
    12.  
    13. End Sub
    14.  
    15. Private Sub Command2_Click()
    16. 'Remove ArrTemp Items no problem here
    17. End Sub

    hope you guys understand what i try to mean

  2. #2
    Addicted Member
    Join Date
    Nov 2006
    Posts
    132

    Re: Array Help

    What is your question?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Array Help

    i have an error subscript out of range. I just don't know how to fix. I know that this error is caused by the array is not dim. but i cannot dimension it. redim preserve either. since others functions access that array and we don't know the len of the array. Also the first time we use the array, there's no item in it, so cannot use preserve

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Array Help

    And I want also ask you guys, if we declare an array like this

    VB Code:
    1. Public arrTemp() as String

    When we unload all forms to quit the program. should we set arrTemp = Nothing in the form unload event

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Array Help

    Close, but no cigar! It should be like this:
    VB Code:
    1. Erase arrTemp

    You must dimension (or ReDim) an array it before you can use it. I presume Form_load would be an appropriate place.

    Either that, or check to see if it has been set up first.. either by having another variable to keep track (possibly a boolean set to True when the array is ReDim'd), or there is a function on the forums you could use, but I cant remember where or who posted it!

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Array Help

    Quote Originally Posted by si_the_geek
    Close, but no cigar! It should be like this:
    VB Code:
    1. Erase arrTemp
    ....
    Yes but you don't need to do that when the program closes because VB will recover the memory you used.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Array Help

    Ah yes, good point - but it should be done when the form unloads (if the array is declared within the form), or at the end of the sub/function if it is declared locally.

    edit: I think that's what I mean... I'm feeling a bit tired/confused right now!

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Array Help

    Quote Originally Posted by si_the_geek
    Ah yes, good point - but it should be done when the form unloads (if the array is declared within the form), or at the end of the sub/function if it is declared locally.

    edit: I think that's what I mean... I'm feeling a bit tired/confused right now!
    I don't think it needs to be done on either condition. In fact I think the only time you need to do it is if you want to clear the array in preparation for it's resuse.

  9. #9
    Addicted Member
    Join Date
    Nov 2006
    Posts
    132

    Re: Array Help

    Quote Originally Posted by MartinLiss
    I don't think it needs to be done on either condition. In fact I think the only time you need to do it is if you want to clear the array in preparation for it's resuse.
    You are correct. The datatype of the array is string, and the memory for the array will automatically be "deallocated" when the array goes out of scope.

  10. #10
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Array Help

    Quote Originally Posted by Vntalk
    i have an error subscript out of range. I just don't know how to fix. I know that this error is caused by the array is not dim. but i cannot dimension it. redim preserve either. since others functions access that array and we don't know the len of the array. Also the first time we use the array, there's no item in it, so cannot use preserve
    Wouldn't a byte array be easier anyway?

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Array Help

    VB Code:
    1. Public colTemp as NewCollection
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. For i = 1 to colTemp.Count ' Note the 1
    6. 'code to compare collection
    7. If  condition = if colTemp(i) then exit sub
    8. Next
    9.  
    10. colTemp.Add "abc [" & "]", "abc [" & "]" ' Add the data, key
    11.  
    12. End Sub
    13.  
    14. Private Sub Command2_Click()
    15. 'Remove ArrTemp Items no problem here
    16. Set ColTemp = Nothing
    17. colTemp = New Collection
    18. End Sub

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Array Help

    may i ask you guys how about remove only the first item in the collection

    like in the collection i have
    collection(1) : abc
    collection(2) : def
    ....
    remove the collection(1) and collection(2) become collection(1) and so on..

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Array Help

    nevermind

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Array Help

    thanks all

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