Arrays

T

Terry

I need to reset an array to its uninitialised state, i.e. nothing in it. How
would I do that please?
Regards
 
G

Gerald Stanley

If the dimensions of the array were set when it was
dimensioned, then you would have to use a For Loop to
initialise each element individually.

If the dimensions were set later using a Redim statement
then the Redim statement could be restated to clear the
array e.g.

Dim strArray() As String

Redim strArray(10)
strArray(1) = "abc"
strArray(2) = "def"
Redim strArray(10)

Hope This Helps
Gerald Stanley MCSD
 
D

Dirk Goldgar

Gerald Stanley said:
If the dimensions of the array were set when it was
dimensioned, then you would have to use a For Loop to
initialise each element individually.

Not necessarily. Check out the Erase statement in the VB online help.
 
T

Terry

Thanks Dirk,
I need to get an existing array back to the state before it was dimensioned,
Erase may appear to do that, I'll give it a go in my context.

The problem I have is in removing rows and resorting the data leaving the
last row to be discarded. ReDim Preserve appears to not only keep the data
but also preserves the array dimensions. i.e. If the uBound(MyArray) gives
3, ReDim Preserve MyArray(1 to 2) does not discard the last row but
preserves everything. I am copying the data into a temp array, selecting a
row to discard, re-sorting downwards to leave the last row unwanted, redim
the source array with one less row, copy the required data back from the
temp array. However I need the original source array to be un-initialised.
Convoluted I know, but it nearly works for me.
Thanks guys
Regards
 
D

Dirk Goldgar

Terry said:
Thanks Dirk,
I need to get an existing array back to the state before it was
dimensioned, Erase may appear to do that, I'll give it a go in my
context.

The problem I have is in removing rows and resorting the data leaving
the last row to be discarded. ReDim Preserve appears to not only keep
the data but also preserves the array dimensions. i.e. If the
uBound(MyArray) gives 3, ReDim Preserve MyArray(1 to 2) does not
discard the last row but preserves everything. I am copying the data
into a temp array, selecting a row to discard, re-sorting downwards
to leave the last row unwanted, redim the source array with one less
row, copy the required data back from the temp array. However I need
the original source array to be un-initialised. Convoluted I know,
but it nearly works for me.
Thanks guys

You're welcome. What you're doing does seem rather convoluted to me. I
don't quite see why you don't either

(A) use a single array, smash the values of the rows you don't want to
keep, sort the array to put those rows at the bottom, and just set a
variable to act as the "logical upper bound" of the array.

or

(B) use a collection instead of an array, and remove the items you don't
want.

Still, good luck with it, however you go about it.
 
T

Terry

hmm, use a collection. I'll have to take a look at that sometime. Got my
scenario to work, not too much overhead in processing as the array size will
be fairly small, no more than 20 rows or so. Couldn't use a variable to hold
the uBound size, too many changes within other functions with the array size
changing all the time. Still the code works allowing me to manage a listbox
using the listbox filling function, filling, sorting and deleting.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top