1

I am using a two-dimensional array of 10 elements, but my code is extremely slow.

Dim myArray As String(,) 
For i=0 to 100
   'Clear Array. (This line is really slow)
   myArray = New String(,) {{"", ""}, {"", ""}, {"", ""}, _
   {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}}

   'Populate array
   fillArray(myArray) 

   'Do stuff with array
   useArray(myArray)
Next i

I create a new empty array with every iteration of the for loop. Is there a way I can use the same array but just clear it instead?

2 Answers 2

3

It's unclear what you're using your array for, but you might want to look into using a collection instead.

As far as clearing the array is concerned, look into using Array.Clear.

Sign up to request clarification or add additional context in comments.

3 Comments

Note, though, that Array.Clear will set the elements to their default value -- which, in the case of strings, is Nothing (null for the C# folks).
The array gets populated and then its contents gets read elsewhere. That bit's not important though. I'm just illustrating the point that I need to clear the array whilst keeping its structure.
What's the advantage of collections over arrays?
1

Doesn't vb.net have a way to quickly create 2 dimensional arrays? It was something like:

Dim rectArray(10, 10) As String

Then you can populate as usual:

Dim rectArray(,) As String = {{'', '', '' ....etc

some reading that might help - link

1 Comment

You're so right! After a bit of research it turns out the syntax is actually arr = New String(9, 1) {}. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.