

MsgBox i & “ “aintInt2 (i) & “ ” & astrStr2(i)Įnd SubThe Redim statement discards the contents of an array if data is already present. MsgBox i & “ “aintInt1 (i) & “ ” & astrStr1(i) ReDim astrStr2 (5) ‘Now a fixed array of strings ReDim aintInt2 (5) ‘Now a fixed array of integers Notice also that, because the Redim statements do not include the LowerBound specifier, each of the dynamic arrays begins at index 0.ĭim aintInt1 (1 to 10) ‘Fixed array of integersĭim astrStr1 (1 to 10) ‘Fixed array of stringsĭim aintInt2 ( ) ‘Dynamic array of integersĭim astrStr2 ( ) ‘Dynamic array of strings

Notice how the dynamic arrays are set to specific sizes by the Redim statement before they are used. You might use fixed and dynamic arrays in an application.

The syntax of these functions is as follows:īecause the bound values of arrays are long integers, both of these functions return long values. The bounds of an array can be determined with the UBound (upper bound) and LBound (lower bound) functions.

You can Redim an array multiple times, dynamically altering the amount of memory occupied by the array. Redim ArrayName (])Īs soon as the Redim statement is applied, Visual Basic allocates as much memory as required to contain the array. Visual Basic recognizes this declaration as a variable-size array and expects you to eventually establish the array size with the Redim statement: In his case, the UpperBound and LowerBound are purposely omitted. However, the extra memory and processing requirements of using variants in an array limit this approach.ĭeclaring a variable-size array requires syntax similar to fixed-length arrays. Of course, when working with an array of variants, each element of the array can hold a different data type value. If this limitation is a problem to you, a collection might be a better solution than an array. The only data limitation of arrays is that all the elements of the array must be of the same data type. As you can see, this program will only allow a user to enter 10 names each time he click on the start button. Private Sub Start_Click() //start command buttonĮnd Sub The above program accepts data entry through an input box and displays the entries in the form itself. Private Sub Exit_Click() //exit command button Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)ĭim Count(100 to 500) as Integer declares an array that consists of the first element starting from Count(100) and ends at Count(500)ĬustName(num) = InputBox("Enter the Customer name", "Enter Name", "", 1500, 4500) Will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CustName(1) to CustName(10). Where subs indicates the last subscript in the array. The general format to declare an array is as follows: The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure. We could use Public or Dim statement to declare an array just as the way we declare a single variable. We differentiate each item in the array by using subscript, the index value of each item. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.įor example, if we need to enter one hundred names, instead of declaring one hundred different variables, we need to declare only one array. When we work with a single item, we only need to use one variable. It means that we have defined the number of elements in the array declaration that will remain the same during the definition of the elements, and its size cannot be changed.By definition, an array is a list of variables, all with the same data type and name. In VB.NET, a fixed- size array is used to hold a fixed number of elements in memory. Dim intArray(,) As Integer = ", i, j, k, threeDimen(i, j, k))Ĭonsole.WriteLine(" Press any key to exit.")
