Zero out Array

  • Thread starter Thread starter DazedAndConfused
  • Start date Start date
D

DazedAndConfused

Is there a quick way to fill an array with zeros or make all elements
empty/nothing?
 
it depends.

specifying the array dimensions in the dim should fill it based on your
requirements.

dim intArray(5) as integer ' elements s/b 0
dim objArray(5) as string ' elements s/b nothing

but then again, i assume you just haven't fleshed out your question
completely.

you can erase arrays, set variables to new arrays, initialize arrays,
etc....it really depends on what it is you're trying to accomplish.

| Is there a quick way to fill an array with zeros or make all elements
| empty/nothing?
 
Your right I didn't give an accurate description of what I am doing.

The array is dimensioned as Shared Friend.
The elements are populated with data and processed.
The array needs to be emptied out before reloading with new data without
redimensioning.

I know I can use for loops to reset the array, but I was looking to see if
there is a better way. I will look to see what erase array offers. Let me
know if there is a better way.

Thank You.
 
ReDim appears to be giving me the result I wanted, but I am unsure if this
is the proper way because I am not really changing dimension sizes. Should I
also be erasing the array before I ReDim?
 
Redim the array without using the 'Preserve' keyword. Numeric types will be
set to zero.
 
Thank you. I get sooo confused with VS Help. ReDim is working great. Help
says "ReDim creates another new array" and I am afraid I am creating
multiple arrays when I only need 1. Just trying not to write crappy code!
 
DazedAndConfused said:
Thank you. I get sooo confused with VS Help. ReDim is working great. Help
says "ReDim creates another new array" and I am afraid I am creating
multiple arrays when I only need 1. Just trying not to write crappy code!

AFAIK, redim will create you another array, but then the first array
will be cleaned up by the garbage collector when it gets around to it
and no other object reference the array.

Chris
 
DazedAndConfused said:
Thank you. I get sooo confused with VS Help. ReDim is working great. Help
says "ReDim creates another new array" and I am afraid I am creating
multiple arrays when I only need 1.

Yes, 'ReDim' will create a new array. However, the old array will be
transparently removed from memory by the garbabe collector automatically.
Alternatively you could loop through the array and reset all the items in
the array (three lines of code...).
 
I have a multi-dimensional array, would I need to:

array.Clear(MyArray,0,MyArray.Length)
array.Clear(MyArray,1,MyArray.Length)
array.Clear(MyArray,2,MyArray.Length)
array.Clear(MyArray,3,MyArray.Length)

?
 
OK, Length is the total size of the array including all dimensions. So
starting at 0 and going to Length would clear all dimensions.

Do I have that right?

Thank you again.
 
Hmm. Probably, you would have to try it out. I dont use multi-dimensional
arrays much, so I havent tried it.
 
It seems to be working. Thank you.
Rick Mogstad said:
Hmm. Probably, you would have to try it out. I dont use
multi-dimensional arrays much, so I havent tried it.
 
Back
Top