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

I thought it was an easy question

P: n/a
Hey all,

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

thanks,
rodchar
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar

Nov 21 '05 #2

P: n/a
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/de...ClassTopic.asp

But back to redim:

The following example from microsoft.com:
http://msdn.microsoft.com/library/de...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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar

Nov 21 '05 #3

P: n/a
lol...
"Imran Koradia" <no****@microsoft.com> wrote in message
news:eZ**************@TK2MSFTNGP10.phx.gbl...
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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar


Nov 21 '05 #4

P: n/a
thanks.

"Imran Koradia" wrote:
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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar


Nov 21 '05 #5

P: n/a
:) some timing huhn ;-)?

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:ee*************@tk2msftngp13.phx.gbl...
lol...
"Imran Koradia" <no****@microsoft.com> wrote in message
news:eZ**************@TK2MSFTNGP10.phx.gbl...
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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar



Nov 21 '05 #6

P: n/a
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

"Imran Koradia" wrote:
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.

"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hey all,

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

thanks,
rodchar


Nov 21 '05 #7

P: n/a
"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
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.
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.