Search Tools Links Login

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


Visual Basic 6, or VB Classic

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!

Original Author: Fosters

Code

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

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 115 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.