article

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

Email
Submitted on: 1/31/2015 8:46:00 AM
By: Fosters (from psc cd)  
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: 2116
 
     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!

 
				

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 23 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


 There are no comments on this submission.
 

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.