# How to empty a multi dimensional array?



## FabRed (Jan 22, 2006)

I want to empty all the contents of a multi dimensional array that I do not know the size of. I tried with .Clear() but that did not work. What is the best way to delete the information in an array?


----------



## cristobal03 (Aug 5, 2005)

What language are you using?

chris.


----------



## FabRed (Jan 22, 2006)

Vb.net


----------



## OBP (Mar 8, 2005)

In visal basic it is erase like

Erase DynamicArray 

the array has to be re Dimmed to llow it work again.


----------



## cristobal03 (Aug 5, 2005)

ReDimming will erase the array anyway, I believe. I posted in another thread discussing the _creation_ of multi-dimensional arrays, it might help a little, let me see if I can find a link...here is that, post #3. If you *ReDim* an array without using the *Preserve* keyword, the array is redimensioned to the new specifications (which wipes everything that was in it before the action).

chris.

[edit]
This post might not apply to .Net, I don't know how strict that language is. You might get an error if you try to ReDim an array without erasing it first, per OBP's suggestion.
[/edit]


----------



## Mithrilhall (Mar 28, 2001)

The next statement resizes the array without saving the contents of the elements.

ReDim MyArray(10) ' Resize to 11 elements.
For I = 0 To UBound(MyArray)
MyArray(I) = I ' Initialize array.
Next I

The following statement resizes the array but saves the contents of the elements.

ReDim Preserve MyArray(15) ' Resize to 16 elements.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmReDim.asp


----------



## cristobal03 (Aug 5, 2005)

> *Originally posted by Microsoft Docs:*
> 
> The next statement resizes the array without saving the contents of the elements.
> 
> ...


I must be misunderstanding the portion in red, because I think I disagree with the quoted statement based on the given code.

The steps in the array process:


```
' declare an array:
Dim varArray() As Variant

' dimension an array:
ReDim varArray(9)

' populate/initialize an array:
For i = 0 To UBound(varArray)
  varArray(i) = i * 2
Next i
```
The populated array is secure until the array is redimensioned or unloaded. A more correct example of *For...Next* erasing the contents of an array would look something like this:


```
Dim varArray() As Variant
Dim i          As Integer

ReDim varArray(0)

For i = 0 To 9
  varArray(i) = i * 2
  ReDim varArray(i + 1)
Next i
```
In the above loop, the array's index would be populated but not preserved as the array grew. You'd end up with a 10-index array full of empty indices.

I realize the quote comes directly from the Microsoft material, but I believe it is misleading on their part because the sample code is broken into pieces. A correct reading would be more along these lines (my changes in blue):


```
' This example uses the ReDim statement to allocate
' and reallocate storage space for array variables.
[color=blue]Private Sub TestArrays()[/color]
  Dim I, MyArray() As Integer   ' Declare variable and array variable.

  ReDim MyArray(5) ' Allocate 6 elements.

  For I = 0 To UBound(MyArray)
    MyArray(I) = I   ' Initialize array.
  Next I

  ' [color=blue]This[/color] next statement resizes the array without
  ' saving the contents of the elements.
  ReDim MyArray(10) ' Resize to 11 elements.
    For I = 0 To UBound(MyArray)
    MyArray(I) = I   ' Initialize array.
  Next I

  ' The following statement resizes the array
  ' but saves the contents of the elements.
  ReDim Preserve MyArray(15) ' Resize to 16 elements.
[color=blue]End Sub[/color]
```
Anyway. Just my thoughts on it.

chris.


----------

