By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,103 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,103 IT Pros & Developers. It's quick & easy.

Simple Array Question

P: n/a
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(intCounter) ' <-- This doesn't work out
'Array(intCounter).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.

Jun 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I tried "Array.Clear()" doesn't work either
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(intCounter) ' <-- This doesn't work out
'Array(intCounter).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.


Jun 22 '06 #2

P: n/a
You don't clear arrays.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"Armand" <ar*************@gmail.com> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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(intCounter) ' <-- This doesn't work out
'Array(intCounter).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.

Jun 22 '06 #3

P: n/a
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(theArray, 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(intCounter) ' <-- This doesn't work out
'Array(intCounter).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.

Jun 22 '06 #4

P: n/a
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(aryStrings, 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(Integer 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.com> wrote in message
news:%2****************@TK2MSFTNGP02.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(theArray, 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(intCounter) ' <-- This doesn't work out
'Array(intCounter).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.

Jun 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.