I’ve to use an array; I don’t know in advance how many elements I need in this array; so I have two options :
First one: to create an array that is larger than the maximum elements I would never have in my program:
Second option: adding one element to the array size each time I need to extend it
Thank you for your help.
First one: to create an array that is larger than the maximum elements I would never have in my program:
Code:
Local lMyArray() As tMyArray Dim lMyArray (1 To 2000)
Code:
Local lMyArray() As tMyArray ' at first just allocate 1 element Dim lMyArray (1 To 1) ... ' and then if I need more elements ' I can use reallocate my array using "REDIM PRESERVE" ReDim Preserve lMyArray (1 To UBound(lMyArray )+1)
- I know it is probably a beginner’s question; can you tell me the pros and cons for each option.
- Is-there any other options?
- Is-it better to allocate more elements each time? if yes I cannot the UBound() function to know how many elements are used in the array? Is-there an alternative in this case?
Thank you for your help.
Comment