Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Size 1

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
GB
I want to creat an array but I am unsure of what size to make. Sometimes the amount of data maybe one or two pieces sometimes it might be 100's how do I define an array with unlimited length
 
I would redefine the size of the array every time I added a new value to the array.

The following example shows the differences between “Redim” and “Redim Preserve”.

Dim A() As Variant 'A() is an undimensioned array, while
'A(2) is a dimensioned array.
Private Sub Command1_Click()
'Redim changes the size of an undimensioned array
ReDim A(0)
A(0) = "Value"
'By using "Redim Preserve", you will protect previosley
'defined values in the array from being deleted
ReDim Preserve A(0)
Debug.Print "A(0)= "; A(0)
ReDim A(0)
Debug.Print "A(0)= "; A(0)
End Sub
 
Hmmmmmm,

KBBJ is correct as far as the post goes. There is a bit more to using arrays in general, and in using the ReDim. If the array is multidimensional, you can only redim the last dimension with preserve. You CAN however create a single dimensional array of UDT's - which 'mimic' a multidimensional array and redim this with the preserve keyword. Other "Features" of arrays abound, and require some careful reading.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
I use 0-based, 1-relative arrays so that UBound(array) = 0 indicates an empty array. I start with a reasonable size, use a variable to keep track of the last filled item, double the size of the array everytime it overflows and resize it to the actual number of items at the end.
Code:
Dim strWhat() as string  ' Dynamic array
Dim J         as long
Redim strWhat(50)        ' Allocate 50 to start 
J = 0 ' J points to last used
Do ......
    read, access data if end exit do
    J = J + 1
    If J > ubound(strWhat) then
        Redim Preserve strwhat (2*J) 
    End if
    strWhat(J)  = somedata
Loop
Redim Preserve strWhat(J) ' Resize to actual
 
John has a good point here. Re-Sizing an array requires re-allocation of 'memory', which takes time. On the other hand, doubling can be a very hazardous process. As one of the kings pharoahs of Egypt is said to have learned when attempting to just double the grains of wheat for each square on a chess board. And ended up owing more grains of wheat than existed in the kingdom (and that was starting with just ONE!

Many of us do the standard 'trick' of just incrementing the array on each pass of adding information. This is inefficient AND fails to reslove the problem of growth without bounds. In many situations, both issues are nit picks, and I have been acused of being one of the pickiest nits around. In 'REAL" programming, the nits are often the difference between sucess and failure, so I can only thank John for - once again - reminding the readers of the pitfalls (and nits to pick?) which abound.

Way back when. In the dark ages of my 'education', we were taught to always estimate the size of things before getting to involved with the details. Granted that these 'estimates' were often wrong, they still served to at least give a reasonable bound or expectation. I often still do this. In EscapeUK's case, it would appear that he does have an upper bound of sorts ("hundreds" should probably NOT go over 1000 - at least without some advance notice / warning), so it might be reasonable to allow the array to start with a number which will hold the majority of his 'occassions' (such as 50?, 25?), and let this double until it was over 1000 (only a FEW times. At that point, I would at least force the user to acknowlege that the process was encountering an unusual situation, even if this is only a msgbox. MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
I tend to initialize arrays to a certain size, and then have an increment value which I add to the arraysize when I hit the boundaries (rather than doubling up). The key point, and I agree with Michael Red here, is to get the starting size correct.

There are a couple of more things to understand about arrays:

You can use IsArray to determine if a variable is an array (rather than rely on the zero'th element)

Arrays are just types of the Variant datatype. Array elements can be variants. Therefore, Array elements can be arrays:

Code:
Dim ar as Variant
ar = array(array(1,2), array(3,4)

creates a 2 dimensional array. Then elements can then be referenced like this:

c(0)(0) = 1
c(0)(1) = 2
c(1)(0) = 3
c(1)(1) = 4

etc. You can redim any dimension like this.

Also, you might want to consider using collections or dictionary objects.

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top