Zero out Array

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?
 
D

DazedAndConfused

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.
 
D

DazedAndConfused

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?
 
H

Hal Rosser

Redim the array without using the 'Preserve' keyword. Numeric types will be
set to zero.
 
D

DazedAndConfused

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!
 
C

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. 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
 
H

Herfried K. Wagner [MVP]

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...).
 
D

DazedAndConfused

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)

?
 
D

DazedAndConfused

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.
 
R

Rick Mogstad

Hmm. Probably, you would have to try it out. I dont use multi-dimensional
arrays much, so I havent tried it.
 
D

DazedAndConfused

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.
 

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