VB.NET Arrays
An array is a linear data structure that is a collection of data elements of the same type stored on a contiguous memory location. Each data item is called an element of the array. It is a fixed size of sequentially arranged elements in computer memory with the first element being at index 0 and the last element at index n – 1, where n represents the total number of elements in the array.
The following is an illustrated representation of similar data type elements defined in the VB.NET array data structure.
In the above diagram, we store the Integer type data elements in an array starting at index 0. It will continue to store data elements up to a defined number of elements.
Declaration of VB.NET Array
We can declare an array by specifying the data of the elements followed by parentheses () in the VB.NET.
In the above declaration, array_name is the name of an array, and the Data_Type represents the type of element (Integer, char, String, Decimal) that will to store contiguous data elements in the VB.NET array.
Now, let us see the example to declare an array.
Initialization of VB.NET Array
In VB.NET, we can initialize an array with New keyword at the time of declaration. For example,
Furthermore, we can also initialize and declare an array using the following ways, as shown below.
Let’s create a program to add the elements of an array in VB.NET programming language.
num_Array.vb
Output:
In the above program, we create an integer array with name marks() and define a For loop to access each item of the array marks.
Input number in VB.NET Array
Let’s create a program to take input values from the user and display them in VB.NET programming language.
Input_array.vb
Output:
Multidimensional Array
In VB.NET, a multidimensional array is useful for storing more than one dimension in a tabular form, such as rows and columns. The multidimensional array support two or three dimensional in VB.NET.
Declaration of Multidimensional Array
In the above representation of multidimensional, we have created 2-dimensional array twoDimenArray with 3 rows and 2 columns and 3-dimensional array with three dimensions 2, 4, and 3.
Initialization of Multidimensional Array
The following ways to initialize the multidimensional array:
Multidimensional Array Example
Let’s create a program to understand the multidimensional array.
MultidimenArray.vb
Output:
Fixed Size Array
In VB.NET, a fixed- size array is used to hold a fixed number of elements in memory. 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. For example, we need to hold only 5 names in an array; it can be defined and initialized in the array such as,
The above representation of the fixed array is that we have defined a string array names 0 to 4, which stores all the elements in the array from 0 to index 4.