Dim and erase a public array at runtime

I am sure this may be quite basic, but am just returning to coding after many years and am stuck on the following issue, so any help would be much appreciated. I am writing a vb.net application that requires a large multi dimensional array which I have initially setup as a public array.

In order to release the memory used, I want to "erase" the array after use. However I may need to reuse the array, so how can I "dim" the array again at runtime, still making it public. I would not need any data retained.

For instance if the array is xx(100,200,50)  is the best way for me to publically dim xx in the design phase and then after use at runtime release its memory using a redim xx(0,0,0) and then redim back to xx(100,200,50) if I need to reuse xx again.

Or is there a much easier way ?

Many thanks for your help

Who is Participating?
Corey ScheichDeveloperCommented:
From my former post
"You can only redim the last dimension of an array IIRC. "

I looked it up and I was partially right this is only the case if you want to preserve the data

For example This is OK

Dim LargeArray () as String

Redim LargeArray (300,200,50)
'do something
Redim LargeArray (400,300,60) 'this will remove all data from the array and resize it

But in the case that you want to preserve the data in the array you can only change the last dimension of the array For example

Dim LargeArray () as String

Redim LargeArray (300,200,50)
'do something
'the following will throw an error
Redim Preserve LargeArray (400,300,60)
'but the following will work
Redim Preserve  LargeArray (300,200,200)

for more information on ReDim look here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmredim.asp



Corey ScheichDeveloperCommented:
You can only redim the last dimension of an array IIRC.

.NET now has ArrayList which is a one dimensional array that automatically updates it's size.

To use a traditional array you could try this

Public SomeArray() as String

Sub CreateLargeArray ()
Dim LargeArray (100,200,50)

'Some code to fill large array

SomeArray = LargeArray

end sub

after the sub is run LargeArray is Garbage Collected and you can empty SomeArray at any time using

SomeArray = Nothing

I would use
  xx = Nothing
to release the resource the array used, then vb .net's  garbage collection function will release the memory.

When you need to use xx again, Dim xx(...) as Array
Hi swtiley,

Expanding on Corey2 example:

Friend SomeArray(,,) as String <<-multidimentional array made available to all

Sub CreateLargeArray ()

Redim LargeArray (100,200,50)

'Some code to fill large array

'Empty the array and free ALL memory associated with the array

Erase LargeArray

'Fill it back up...
Redim LargeArray (200,300,50)

'Fill it with more data
Expand the array but 'Preserve' the current data
Redim Preserve  LargeArray (300,400,100)

' add mopde data

end sub

Hope this helps,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.