I thought it was an easy question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey all,

How do I add an element to an already populated array?
dim myary() as string

thanks,
rodchar
 
Dim myarray() As Integer = {1, 2}
ReDim Preserve myarray(2)
myarray(2) = 3

Don't forget the preserve word or else all the elements will be lost from
the array. Also, you would be better off using an ArrayList if you are going
to be adding elements frequently since Redim Preserve is expensive
performance-wise (especially with larger arrays).

hope that helps..
Imran.
 
Hi rodchar

use "redim preserve".

Beware that this can be inefficient. If you are having to do it frequently,
use a System.collection type such as arraylist.
http://msdn.microsoft.com/library/d...frlrfSystemCollectionsArrayListClassTopic.asp

But back to redim:

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

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 IThe 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 IThe following statement resizes the array but saves the contents of
the elements.

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


Imran Koradia said:
Dim myarray() As Integer = {1, 2}
ReDim Preserve myarray(2)
myarray(2) = 3

Don't forget the preserve word or else all the elements will be lost from
the array. Also, you would be better off using an ArrayList if you are
going
to be adding elements frequently since Redim Preserve is expensive
performance-wise (especially with larger arrays).

hope that helps..
Imran.
 
thanks.

Imran Koradia said:
Dim myarray() As Integer = {1, 2}
ReDim Preserve myarray(2)
myarray(2) = 3

Don't forget the preserve word or else all the elements will be lost from
the array. Also, you would be better off using an ArrayList if you are going
to be adding elements frequently since Redim Preserve is expensive
performance-wise (especially with larger arrays).

hope that helps..
Imran.
 
how do you clear an entire array. And if you clear an array would you still
have to redim it if you filling it up with more than it had prior to clear?

thanks,
rodchar
 
rodchar said:
how do you clear an entire array. And if you clear an array would you still
have to redim it if you filling it up with more than it had prior to clear?

thanks,
rodchar

Two ways:
1. myArray.Clear
This clears the elements of the array leaving the array elements in place.
In this case, yes, you will have to ReDim if you want to add more elements
than are already in it.

2. Erase myArray
This will nullify the array object deleting all elements in it. You'll have
to define the array again to use it.

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Subtotal by VBA 5
Weird behavior on page when trying to retrieve data from Session 2
database records into an array 2
search my String[] 6
2 arrays into 1 2
Array question 3
string formatting 5
enum basics 7

Back
Top