VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



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

by Fosters (24 Submissions)
Category: Data Structures
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

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!

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

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
 

Download this snippet    Add to My Saved Code

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

No comments have been posted about Dynamically allocate arrays when you don't know how many items to store. Why not be the first to post a comment about Dynamically allocate arrays when you don't know how many items to store.

Post your comment

Subject:
Message:
0/1000 characters