Dim a() ' variable length array of variants Dim b(5) as String ' An array of 5 strings Dim c(3 to 8) as Integer ' 6 elements Dim d(4, 7) ' Multiple dimensions, up to 60 allowed for i from 1 to 5 a(i) = i next iUse Option Base to set the default starting index value. If Option Base is not set, the default is zero.
IsArray | Test the variable type |
Array | Used to assign multiple values in one statement |
Option Base | Set the default lower index limit |
LBound UBound | Determine the upper and lower bounds of the specified dimension |
Erase | Clear the array's contents |
Redim | Change the size of the array |
Dim Private Public Static | Declare the array |
Dim a() ' variable length array of variants ReDim a(10, 3 to 8) ' Produces 2 dimensions and clears all data ReDim Preserve b(200) ' Keeps the old values ReDim Preserve c(UBound(c) + 50) ' Add 50 more slotsYou can not re-size an array passed as an argument. (Funny, I read this in the help but could not find it again! Even funnier, re-size seems to work fine.)
There is no command to determine if a dynamic array has been allocated space. This is important because UBound(c) fails with a "Subscript out of Range" error if no memory is allocated. I've tested
|
Type TUser ' T means Type i1 As Integer i2 As Integer End Type Dim C() As TUser |
Jostein Trondal has suggested the following work around which works in VB 6.0.
Public Function UUBound(a As Variant) As Long On Error Resume Next Dim size As Long size = UBound(a) If Err = 9 Then UUBound = -1 Else UUBound = size End If End Function
Duncan Drysdale has suggested the following to test for a blank or null VB dynamic array
if ( not arrayname ) = true then msgbox "I am empty..." else msgbox "Wee hee! I have stuff in me! " end if
array [index-type] of element-type type IntList = array[1..100] of Integer; CharData = array['A'..'Z'] of Byte; TMatrix = array[0..9, 0..9] of real; var MyArray: array[1..100] of Char; Camera : array[1..4] of TEERCamera; M1 : TMatrix ; // The variable M1 is an array In code M1[2,4] is equivalent to M1[2][4] Arrays of records Results[1].CountIf an array [1..30] is passed to a subroutine, inside the subroutine, the first element is zero (0).
High() & Low()
type TColorArray = array[0..155, 0..255, 0..255, 1..3] of byte; var a2: TColorArray ; begin i := High(a2); // This returns the value 155 i := High(a2[0,0,0]); // This is equivalent to High(a2[0][0][0]) // Both return the value 3
Variable Length Arrays
Variable length array type data can be stored in
Collection | List | String | Outline |
---|---|---|---|
Count Items | Count Items List Capacity | Count Objects Strings Values | Text Data Index FullPath Expanded ... |
Add Clear BeginUpdate EndUpdate | Expand Pack Add Delete Insert Remove Move Exchange | Add Append Delete Insert Move Exchange Clear BeginUpdate EndUpdate SaveToFile LoadFromFile | FullExpand Collapse Expand |
type var MyFlexibleArray: array of Real; SetLength(MyFlexibleArray, 20); A := Copy(A, 0, 20) truncates all but the first 20 elements of A. length(a) number of elements high(a) largest index low(a) lowest index
tempStr := a.name[i]; // Returns the key at i j := a.IndexOfName('keyName'); // Returns the location of keyName tempStr := Values['keyname']; // Returns the associated value function IndexOfName(const Name: string): Integer property Values[const Name: string]: string; property Names[Index: Integer]: string; // Returns the Name at that index
long varName[8]; // Contains 8 values - 0 thru 7 short b[] = {2, 5, 1, 7}; // Create and initialize array with 4 elements short* bb = b; // b without a subscript is the address // of the 1st element of the array long c[4][3]; short* xx = new short[x]; // Allocate an array at run time char str[] = "Any String"; // Stores a null terminated string for ( int i = 0; i < 10; i++ ) { a[i] = i; }C++ does not check array bounds. If you try to write past the end of an array, memory will be modified! But since the memory is not allocated to the array, the results are un-predicable.
2 different syntaxes can be used to declare arrays
int[] array1; int array1[];However, this does not allocate any memory.
int[] array1 = new int[200]; int[] array2 = {30,31,29,30,30}; int[][] a1 = new int[6][3]; int[][] a1 = new int[6][]; // Each element can have a different a1[1] = new int[5]; // number of elements a1[2] = new int[8]; a1[2][4] = 20; array1.length returns the number of elements