Important alert: (current site time 7/16/2013 12:52:16 AM EDT)
 

article

__ Dynamically allocate arrays when you don't know how many items to store!

Email
Submitted on: 3/4/2003 4:13:13 AM
By: Fosters 
Level: Beginner
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 32250
(About the author)
 
     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...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. 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 

    sRecieve1DArray = ReturnOneDimensionalArray 
    Debug.Print UBound(sRecieve1DArray) Debug.Print sRecieve1DArray(0), sRecieve1DArray(1), sRecieve1DArray(2) 

    sRecieve2DArray = ReturnTwoDimensionalArray 
    Debug.Print UBound(sRecieve2DArray, 2) 
    Debug.Print sRecieve2DArray(0, 0), sRecieve2DArray(0, 1), sRecieve2DArray(0, 2) 
    Debug.Print sRecieve2DArray(1, 0), sRecieve2DArray(1, 1), sRecieve2DArray(1, 2)
End Sub

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) 

        'populate into the upper limit -1 
        sTempArray(0, UBound(sTempArray, 2) - 1) = Chr(65 + iCount) 
        sTempArray(1, UBound(sTempArray, 2) - 1) = Chr(97 + iCount) 
        iCount = iCount + 1 
    Loop Until iCount >= 26 

    '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


Other 36 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/21/2003 9:27:24 AM

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.)

 
9/5/2005 7:16:35 PMAlejandro Aleman

man, i did not ask if i could find it..

How can i build a 2d array an initialize wit content whithout typing like this:

a(0,0) = 0
a(0,1) = 1
..
a(14, 4) = 44

just like c or java?

array= { { 0,1},....,{0,1}}
(If this comment was disrespectful, please report it.)

 
9/30/2005 1:28:08 AMalex

utot
(If this comment was disrespectful, please report it.)

 
2/17/2007 6:39:11 AMkeval

hi i like your coding very nice logic.
can i call to u ?

(If this comment was disrespectful, please report it.)

 
8/2/2007 12:06:14 PMParvees

Well. This is an interesting program
(If this comment was disrespectful, please report it.)

 
2/12/2010 9:02:59 AMAnjum Ashraf

please send new codes
(If this comment was disrespectful, please report it.)

 
3/2/2010 8:08:31 PMjayed

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.)
 

To post feedback, first please login.