473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I thought it was an easy question

Hey all,

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

thanks,
rodchar
Nov 21 '05 #1
7 1008
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
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
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
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
:) 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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

100
by: Peter | last post by:
Company thought DB2 will be better than Oracle. The bottom line is when you do select, the system crash. I think it may take 4-5 years for DB2 to reach Oracle standard. Peter
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
5
by: tHeRoBeRtMiTcHeLL | last post by:
Well, I think I have bitten off a little more than I can chew (at least all at once), and I'm only trying to hammer out tables/relationships at the design level. Which translates to "Seasoned...
6
by: Rob Bolton | last post by:
Hi there, If a program running under the interactive logon session (say Susan), needs to impersonate Bob (via "LogonUser()"), but Bob needs to access the network as Susan (i.e., his local...
5
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one...
22
by: Charles Law | last post by:
Could someone please explain to me, in words of one syllable or less, how I get the Validating event to fire for a form. I have a form with one text box, and two buttons: OK and Cancel. I have...
13
by: Ghislain Tanguay | last post by:
I have a compiled vb.net app and I want to give the user a choice to launch it from the start line command and pass it a parameter or not. How can I do that in my code? Is it possible? Ex. :...
1
by: melanieab | last post by:
Hi, If there's a textbox and the text entered is longer than what's visible (the textbox length), how do you make it so that the beginning chunk of text is visible (instead of the last part of...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
14
by: EJ | last post by:
I'm real new to javasscript. The task is to develop a web page which will allow users to customize a laptop ... show the price as they change options ... transfer the price and system description...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.