All programmers need to allocate arrays to store data, and very often they don't know how much they will be storing. here is a beginners tutorial that shows how to dynamically allocate an array on the fly that will only allocate as many items as are needed!
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
This is a short tutorial on dynamically building arrays
(with examples for 1 and 2 dimensions).
There are many occasions where you need
to allocate an array, but don't know what the upper bounds are.
Shown here is an efficient tried and trusted method.
The whole concept revolves around UBOUND - the upper limit of your array.
Knowing the upper limit allows you to increase it's size by as much as
you need to, without having to initially allocate a huge
array at the start!
The key points are
'define a 0 bounded array, so that redims later on do not fail
ReDim sTempArray(0)
'perform your loop to work out what must go in each element of your array
Do If we need to allocate another item to the array Then 'redimension the array to accomodate the new data ReDim Preserve sTempArray(UBound(sTempArray) + 1)
sTempArray(UBound(sTempArray) - 1) = ???
End If
Loop Until ???
'this method allocates 1 too many array items, so reduce it by 1
ReDim Preserve sTempArray(UBound(sTempArray) - 1)
'Array is ready to be returned with only the data you have allocated
You can paste the following example code to an app.
run your app, Pause it, and in the immediates window, type GetArrayData
Sub GetArrayData()
Dim sRecieve1DArray() As String
Dim sRecieve2DArray() As String
Function ReturnOneDimensionalArray() As String()
Dim sTempArray() As String
Dim iCount As Integer
'initially define the array otherwise the other redims will fail ReDim sTempArray(0) iCount = 0
Do 'redimension the array to the upper limt + 1
ReDim Preserve sTempArray(UBound(sTempArray) + 1)
'populate into the upper limit -1
sTempArray(UBound(sTempArray) - 1) = Chr(65 + iCount)
iCount = iCount + 1 Loop Until iCount >=
26
'you have 1 more index than necessary, so reduce it by 1 ReDim Preserve sTempArray(UBound(sTempArray) - 1)
'assign the temporary array to the function for return
ReturnOneDimensionalArray = sTempArray
End Function
Function ReturnTwoDimensionalArray() As String()
Dim sTempArray() As String
Dim iCount As Integer
'initially define the array otherwise the other redims will fail 'remember, you can only redim the last dimension ReDim sTempArray(2, 0)
iCount = 0 Do
'redimension the array to the upper limt + 1 'you are referencing and increasing the 2nd dimension ReDim Preserve sTempArray(2, UBound(sTempArray, 2) + 1)
'you have 1 more index than necessary (on the 2nd dimension), so reduce it by 1 ReDim Preserve sTempArray(2, UBound(sTempArray, 2) - 1)
'assign the temporary array to the function for return
ReturnTwoDimensionalArray = sTempArray End Function
Thanks for this nifty code, as a biginner this really helps me and others alot! Can you store elements form a list box or any other control in a dynamic array? (If this comment was disrespectful, please report it.)
3/21/2003 6:08:51 PM:
Hi me again, figured out how to add elements from a list box or any other control in that matter into a dynamic array. Great code for learning... helps you figure out what to do on your own. If your persistant of course! :) (If this comment was disrespectful, please report it.)
4/30/2003 7:39:19 AM:
really good -thanks
(If this comment was disrespectful, please report it.)
4/21/2004 9:26:39 AM:
toan
(If this comment was disrespectful, please report it.)
2/24/2005 1:56:13 PM:
Simple and works well. This is exactly what I needed. (If this comment was disrespectful, please report it.)
3/6/2005 1:09:25 PM:
THIS SITE VERY GOOD FOR FREASHER HOW CAN I GET FULL CODE OF DATASTRUCTURE AT A TIME
(If this comment was disrespectful, please report it.)
I am thank full to you,for giving me that.i want more coding. (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)