Library tutorials & articles
Arrays
Dynamic Arrays
When using arrays you must be careful not to consume too much memory. For example
Dim MyArray (10000) As Long
Because each element is declared as long, and a long variable occupies 4 bytes of memory, the MyArray requires 40 004 (10 001 x4) bytes of memory. This may not sound like much, but when you have 10 such arrays in your program these arrays consume 400 040 (40 004 x 10) bytes of memory. Therefore, it is wise to always try to set the size of your arrays to the minimum your program requires. Sometimes, however, it is only possible to detemine this during runtime. In these cases you can use the ReDim statement to change the size of an array. An array that changes its size during runtime is called a dynamic array.
When you declare a dynamic array, you do not declare it like a fixed array. When you declare a dynamic array the size is not specified. Instead you use the following syntax:
Dim ArrayName() As DataType
Dim is the scope of the array. If you declare it in a Form, use Dim, if you declare it in a Module and you want every procedure to access it, declare it as Global, otherwise use Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. DataType is a valid datatype. Normally Integer (-32 768 to 32 767), String (A string of characters), Boolean (True or False).
You then use the ReDim statement in your procedures or functions, using the following syntax:
ReDim ArrayName(LowerValue To HigherValue)
In fact, it is almost identical to a normal declaration for a fixed array, except that 1), it is not a declaration, as it is executed at runtime, 2) it uses ReDim, and 3) there is no datatype declaration (this cannot be changed).
So, the following code declares a dynamic array called gArray, and then sets the size during runtime:
Dim sStringArray() As String
Sub Form1_Load()
' Initialise array
ReDim sStringArray(1 To 10)
End Sub
This code assigns 10 elements to gArray when Form1 loads. Note: When using dynamic arrays, you must set the size of an array using the ReDim statement, before filling the array.
However, when using the ReDim statement, any values already in the array (if it has been resized previously), will be deleted. In some cases, this is not what you would want! So, you use the Preserve keyword:
ReDim Preserve ArrayName(LowerValue To HigherValue)
If the array has grown, there will be a number of blank array spaces at the end of the array. If the array has shrunk, you will lose the end items.
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
I'm a bit confuse. I would like user to insert how many data they want to put in the array.
When I declare the array like this
Dim subjArr( ) As DataType
How should I write the code so array size is according to user input. This is the code for user to insert how many records should be inserted.
Dim SubArr() As DataType
Dim subjNo As String
subjNo = InputBox("Enter the no of subjects to calculate: ")
MsgBox (subjNo + " subjects will be calculated")
Thanks James
This is a good Array Tutorial for VB Newbies like me.
http://www.4guysfromrolla.com/webtech/110800-1.shtml
I really just added this in case anyone comes across this in a search as I did
+++++++++++++++
check it: FLEETING IMAGE
No, I believe he means something like this:
Say you had a string array with 6 elements, beginning with zero.
The values were arranged like this:
strArray(0) = "Step 1"
strArray(1) = "Step 2"
strArray(2) = "Step 3"
strArray(3) = "Step 4"
strArray(4) = "Step 5"
strArray(5) = "Step 6"
He wants to randomly arrange the elements' contents, like this:
strArray(0) = "Step 4"
strArray(1) = "Step 3"
strArray(2) = "Step 5"
strArray(3) = "Step 1"
strArray(4) = "Step 6"
strArray(5) = "Step 2"
I hope this is correct, and/or clear.
Are you speaking of randomly pulling one of the values as in say a random name generator?
Can anyone tell me if it's possible to randomly sort the elements in an array?
I'm not familiar to 3d arrays, but could you use something similar to:
I use that to copy 1d arrays.
Not the best piece of code but meh.
Public Sub MoveArray()
For Z = 0 To 10
Array2(Z) = Array1(Z)
Next Z
End Sub
I am dealing with some very large (3 dimensional) arrays and the above kind of operation seems a little slow for the user to sit around waiting for. (Especially when it will need to be used in the Undo Function)
Any suggestions will be gratefully received.
couling@fsnet.co.uk">pcouling@fsnet.co.uk
This thread is for discussions of Arrays.