In either case, it doesn't matter. An array is immutable. So, it is either
referenced or not. Once you create an array, you can neither make it shorter
or longer. Setting it to a null (Nothing) value has the same effect as
de-referencing it. For example:
Dim aryStrings() As String = {"Short", "Lived", "Array"}
aryStrings = {"Some", "New", "Array"}
What you have done here is not to replace the elements in the array, but to
de-reference the first array by assigning a new array to the variable. On
the other hand,
aryStrings(0) = "Another"
only replaces the string in the first element of the array.
By the same token:
Dim aryStrings(3) As String()
creates an array of 3 elements. When you write
aryStrings = New String(4)
de-references the original array of 3 elements and replaces it with a new
array of 4 elements.
Now, the Clear method of the Array class is a static (Shared) method, which
is invoked without a reference to the array itself, except as a parameter:
Array.Clear(ary Strings, 0, 4)
does not shorten the array. It simply sets the members of the array to null
values (Nothing). The array is still 4 elements in length.
When the de-referenced array which the variable used to reference is
de-referenced by assigning another array to the variable, it is cleaned up
from memory, regardless of whether you set it to Nothing or not. An array
(or anything else) can also be de-referenced by passing out of scope, as in:
Private Sub ReplaceFirst(In teger i)
Dim aryIntegers as { 0, 1, 2 }
aryIntegers(0) = i
End Sub
As soon as the Sub exits, the array created inside it is de-referenced and
cleared from the stack. The same thing will happen if, for an example, a
class has a member that is an Array, and the class is de-referenced. Since
the Array was a member of the class, and the class is de-referenced, the
Array is also de-referenced.
Only Collections can be resized.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
I recycle.
I send everything back to the planet it came from.
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
What do you mean by clearing the array?
If you want to set every value in the array to Nothing, the Clear method
works. You call it like this:
Array.Clear(the Array, 0, theArray.Length )
If you want to get rid of the entire array, just set the reference to
Nothing:
theArray = Nothing
Armand wrote: Hi Guys,
I have a set of array that I would like to clear and empty out.
Since I am using "Array" not "ArrayList" , I have been struggling in
finding the solution which is a simple prob for those who experience.
(For some reason I have to implement Array not ArrayLists)
Below are the simple following code:
Dim Array() As String
Dim intCounter As Integer
Array = strRESP.Split(" ,") ' <-- insert value which is comma
deliminated
........... ' It has been working then I would like to clear the array
...........
For intCounter = 0 To Array.Length - 1
Array.Clear(int Counter) ' <-- This doesn't work out
'Array(intCount er).Clear() <-- This method doesn't work
either
Next
Do you guys have any solution/ recommendation to clear those array
lists?
I really appreciate all of your input.