|
-
Dec 15th, 2006, 12:38 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Array Help
Here's an example of what i try to archive
VB Code:
Public arrTemp() as String
Private Sub Command1_Click()
'Cannot Redim Preserve here since i don't know the size
For i = 0 to Ubound(arrTemp)
'code to compare array
If condition = arrTemp(i) then exit sub
Next
ReDim Preserve arrTemp(UBound(arrTemp) + 1)
arrTemp(UBound(arrTemp)) = "abc [" & "]"
End Sub
Private Sub Command2_Click()
'Remove ArrTemp Items no problem here
End Sub
hope you guys understand what i try to mean
-
Dec 15th, 2006, 12:45 PM
#2
Addicted Member
-
Dec 15th, 2006, 12:50 PM
#3
Thread Starter
Addicted Member
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
-
Dec 15th, 2006, 12:54 PM
#4
Thread Starter
Addicted Member
Re: Array Help
And I want also ask you guys, if we declare an array like this
VB Code:
Public arrTemp() as String
When we unload all forms to quit the program. should we set arrTemp = Nothing in the form unload event
-
Dec 15th, 2006, 12:59 PM
#5
Re: Array Help
Close, but no cigar! It should be like this:
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!
-
Dec 15th, 2006, 02:01 PM
#6
Re: Array Help
 Originally Posted by si_the_geek
Close, but no cigar! It should be like this:
....
Yes but you don't need to do that when the program closes because VB will recover the memory you used.
-
Dec 15th, 2006, 02:18 PM
#7
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!
-
Dec 15th, 2006, 02:23 PM
#8
Re: Array Help
 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.
-
Dec 15th, 2006, 05:24 PM
#9
Addicted Member
Re: Array Help
 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.
-
Dec 15th, 2006, 06:16 PM
#10
Hyperactive Member
Re: Array Help
 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?
-
Dec 15th, 2006, 06:23 PM
#11
Re: Array Help
VB Code:
Public colTemp as NewCollection
Private Sub Command1_Click()
For i = 1 to colTemp.Count ' Note the 1
'code to compare collection
If condition = if colTemp(i) then exit sub
Next
colTemp.Add "abc [" & "]", "abc [" & "]" ' Add the data, key
End Sub
Private Sub Command2_Click()
'Remove ArrTemp Items no problem here
Set ColTemp = Nothing
colTemp = New Collection
End Sub
-
Dec 15th, 2006, 07:14 PM
#12
Thread Starter
Addicted Member
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..
-
Dec 15th, 2006, 07:18 PM
#13
Thread Starter
Addicted Member
-
Dec 15th, 2006, 08:37 PM
#14
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|