__ Dynamically allocate arrays when you don't know how many items to store!
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!
AI
ИИ-обзор: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Исходный код
<P><FONT face="Courier New" size=2>This is a short tutorial on dynamically building arrays <br> (with examples for 1 and 2 dimensions).<br> There are many occasions where you need <br> to allocate an array, but don't know what the upper bounds are.<br> Shown here is an efficient tried and trusted method.<br> The whole concept revolves around UBOUND - the upper limit of your array.<br> Knowing the upper limit allows you to increase it's size by as much as <br> you need to, without having to initially allocate a huge <br> array at the start! <br><br> The key points are <br> 'define a 0 bounded array, so that redims later on do not fail<br> ReDim sTempArray(0) <br> 'perform your loop to work out what must go in each element of your array <br> Do <br> If we need to allocate another item to the array Then <br> 'redimension the array to accomodate the new data <br> ReDim Preserve sTempArray(UBound(sTempArray) + 1) <br> sTempArray(UBound(sTempArray) - 1) = ??? <br> End If <br> Loop Until ??? <br><br> 'this method allocates 1 too many array items, so reduce it by 1 <br> ReDim Preserve sTempArray(UBound(sTempArray) - 1) <br> 'Array is ready to be returned with only the data you have allocated<br><br> You can paste the following example code to an app. <br> run your app, Pause it, and in the immediates window, type GetArrayData <br><br> Sub GetArrayData() <br> Dim sRecieve1DArray() As String <br> Dim sRecieve2DArray() As String <br><br> sRecieve1DArray = ReturnOneDimensionalArray <br> Debug.Print UBound(sRecieve1DArray) Debug.Print sRecieve1DArray(0), sRecieve1DArray(1), sRecieve1DArray(2) <br><br> sRecieve2DArray = ReturnTwoDimensionalArray <br> Debug.Print UBound(sRecieve2DArray, 2) <br> Debug.Print sRecieve2DArray(0, 0), sRecieve2DArray(0, 1), sRecieve2DArray(0, 2) <br> Debug.Print sRecieve2DArray(1, 0), sRecieve2DArray(1, 1), sRecieve2DArray(1, 2) <br> End Sub <br><br> Function ReturnOneDimensionalArray() As String() <br> Dim sTempArray() As String <br> Dim iCount As Integer <br><br> 'initially define the array otherwise the other redims will fail <br> ReDim sTempArray(0) <br> iCount = 0 <br><br> Do <br> 'redimension the array to the upper limt + 1 <br> ReDim Preserve sTempArray(UBound(sTempArray) + 1)<br><br> 'populate into the upper limit -1 <br> sTempArray(UBound(sTempArray) - 1) = Chr(65 + iCount) <br><br> iCount = iCount + 1 <br> Loop Until iCount >= 26<br><br> 'you have 1 more index than necessary, so reduce it by 1 <br> ReDim Preserve sTempArray(UBound(sTempArray) - 1) <br><br> 'assign the temporary array to the function for return <br> ReturnOneDimensionalArray = sTempArray <br> End Function <br><br> Function ReturnTwoDimensionalArray() As String() <br> Dim sTempArray() As String <br> Dim iCount As Integer <br><br> 'initially define the array otherwise the other redims will fail <br> 'remember, you can only redim the last dimension <br> ReDim sTempArray(2, 0) <br><br> iCount = 0 <br> Do <br><br> 'redimension the array to the upper limt + 1 <br> 'you are referencing and increasing the 2nd dimension <br> ReDim Preserve sTempArray(2, UBound(sTempArray, 2) + 1) <br><br> 'populate into the upper limit -1 <br> sTempArray(0, UBound(sTempArray, 2) - 1) = Chr(65 + iCount) <br> sTempArray(1, UBound(sTempArray, 2) - 1) = Chr(97 + iCount) <br> iCount = iCount + 1 <br> Loop Until iCount >= 26 <br><br> 'you have 1 more index than necessary (on the 2nd dimension), so reduce it by 1 <br> ReDim Preserve sTempArray(2, UBound(sTempArray, 2) - 1) <br> 'assign the temporary array to the function for return <br> ReturnTwoDimensionalArray = sTempArray <br>End Function</FONT> </P>
Оригинальные комментарии (3)
Восстановлено из Wayback Machine