Link to home
Start Free TrialLog in
Avatar of dgb001
dgb001

asked on

--------------- RE-DIMMING AN ARRAY ----------------

I have a two-dimensional public array, and want to be able to re-initailize it at given points in the program. I know that it's easy enough just to loop round and clear everything out, but can't help thinking there must be a proper way. I know that you can change the number of elements by using the REDIM statement, but I don't think that I can redim it to the same parameters, and thus clear it. I must admit I haven't tried this for a couple of months, so I'm talking about this from memory, but I know that I'm going to have to do the same thing again shortly. Any comments appreciated, but please don't log your comment as an answer unless you're 100% sure - this way other people get the chance to respond too. Thanks, Dave
Avatar of ChasDevlin
ChasDevlin
Flag of United Kingdom of Great Britain and Northern Ireland image

ReDim ArrayName(50) - this sets everything back to defaults i.e. zeroes or ""

ReDim Preserve(50) = this keeps your CURRENT values intact.


Avatar of vmano
vmano

to clear all the contents in an array use Erase statement:
Erase arraylist
The required arraylist argument is one or more comma-delimited array variables to be erased and then ReDim the Array

hope this helps
vmano


Avatar of dgb001

ASKER

Ok guys, I'll try out your suggestions. Thanks for taking the time to add your comments.

ChasDevlin had it with a modification..
Dim ArrayName as Variant

>>ReDim ArrayName(50) as Variant - this sets everything back to defaults i.e. zeroes or "" 

>>ReDim Preserve ArrayName(51) as Variant = this keeps your CURRENT values intact and adds a element.

Don't forget also, you can only redim an array in one dimension.  The other dimension must stay constant.
Avatar of dgb001

ASKER

Well, thanks for all the comments, but the one by vmano proved the most useful for my particular purposes. Using the erase statement does exactly what I want, and also you don't need to redim the array afterwards, it just clears the existing one. Many thanks.
Dave

dbg001,
can i post my comment as answer???

vmano
Avatar of dgb001

ASKER

Yes, sure vmano. Thanks for your help.
Dave

ASKER CERTIFIED SOLUTION
Avatar of vmano
vmano

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial