Reset a dynamic array - Redeem or Erase?

Asked

Viewed 574 times

1

I have a UserForm with three OptionButton and three ListBox.

When opening the UserForm all three ListBox are loaded with different texts, because each one relates to a type of data as is explicit in each OptionButton.

Us ListBox it is allowed to select more than one option.

The ListBox are disabled at the opening of the UserForm.

By clicking on the first OptionButton, the code to which it refers removes the selections (if any) from the others ListBox, and disable them, the same will happen if you click on one of the others OptionButton.

By clicking the UPDATE button, the code of the same checks which OptionButton is active, and runs through the ListBox corresponding by filling a dynamic array (array) with the name of the selected texts.

The code works perfectly, already well tested. My question: clicking on any of the OptionButton, the array has to be emptied and no size to be filled again from scratch, so I can use ReDim(0) or Erase Array.

Which one should I wear?

According to Internet searches, I believe it’s Erase, because the information is that wipes the memory to use the array again.

inserir a descrição da imagem aqui

  • 1

    Erase is usually used before Redim, since the latter resizes the array by holding the values.

  • OK. Grateful for the answer. But I want to not only erase the data from the array, but also resize it to zero.

  • Only the Erase is sufficient.

1 answer

1

Redeem and Erase

The best would be to follow the semantics, so that someone else who reads the code or yourself can understand more easily and help in maintaining the code in the future.

Then the function Redeem is used to scale or resize a dynamic matrix and Erase to release matrix variables and displace the memory used for their elements.

In addition to the fact that Resize cannot be used in static Arrays, Erase is used in both cases (static and dynamic).

Example

See the following Soen example to check that the only difference is in dynamic arrays:

'https://stackoverflow.com/a/38128773/7690982
Dim NumArray(10) As Integer ' Integer array.
Erase NumArray ' Each element set to 0.

Dim StrVarArray(10) As String ' Variable-string array.
Erase StrVarArray ' Each element set to zero-length string ("").    

Dim StrFixArray(10) As String * 10 ' Fixed-string array.
Erase StrFixArray ' Each element set to 0.

Dim VarArray(10) As Variant ' Variant array.
Erase VarArray ' Each element set to Empty.

Dim DynamicArray() As Integer ' Dynamic array.
ReDim DynamicArray(10) ' Allocate storage space.
Erase DynamicArray ' Free memory used by array.

Dim DynamicArray2() As Integer ' Dynamic array.
ReDim DynamicArray2(10) ' Allocate storage space.
ReDim DynamicArray2(0) ' Allocate storage space.

The result in the Local Variables window for each array:

  • Numarray:

NumArray

  • Strvararray:

StrVarArray

  • Strfixarray:

StrFixArray

  • Vararray:

VarArray

  • Dynamicarray:

DynamicArray

  • Dynamicarray2:

DynamicArray2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.