Review
Best Keyboard for Programming & Coding in 2021
Programmers spend most of their days on a computer designing, writing, and testing code. This...
An array is a data structure used to store elements of the same data type. The elements are ordered sequentially with the first element being at index 0 and the last element at index n-1, where n is the total number of elements in the array.
In this tutorial, you will learn:
In VB.NET, arrays are declared using the Dim statement. For example:
Dim myData() As Integer
In the above example, we have defined an array named myData, and it should hold elements of the integer data type. The following example demonstrates how we can declare an array to hold string elements:
Dim myData(10) As String
We have defined an array named myData to hold 10 strings.
We can initialize arrays at the time of their declaration. For example:
Dim myData() As Integer = {11, 12, 22, 7, 47, 32}
We have declared the array myData and added 6 integer elements to it. This can also be done for the case of strings:
Dim students() As String = {"John", "Alice", "Antony", "Gloria", "jayden"}
We have created an array named students and added 5 names to it.
A fixed-size array holds a fixed number of elements. This means that you must define the number of elements that it will hold during its definition. Suppose you need an array to hold only 3 student names. You can define and initialize the array as follows:
Dim students(0 to 2) As String
students(0) = "John"
students (1) = "Alice"
students (2) = "Antony"
We began by declaring a string array named students. The 0 to 2 declares that the array will store elements from its index 0 to index 2, meaning we will have 3 elements in total.
To add elements to the array, we have used the array name and specified the index at which the element will be stored. For example, the name John will be stored at index 0 of the array, meaning that it will form the first element of the array. Antony will be the last element of the array.
This is an array that can hold any number of elements. The array size can grow at any time. This means that you can add new elements to the array any time we want. To demonstrate this, let us first define an array of integers:
Dim nums() As Integer
We have defined an integer array named nums. You now need to add two elements to the array, while giving room for resizing it. You need to use the ReDim statement as follows:
ReDim nums(1) nums(0) = 12 nums(1) = 23
Our array now has two elements in indexes 0 and 1. We need to add a third element to it at index 3 while preserving the two element it already has. We can do it as follows:
ReDim Preserve nums(2) nums(2) = 35
The array now has three elements.
Retrieving means accessing the array elements. To access an array element, we use its index. Let us demonstrate this using an example.
Step 1) Begin by creating a new console application.
Step 2) Add the following code to the application:
Module Module1
Sub Main()
Dim students(0 to 2) As String
students(0) = "John"
students(1) = "Alice"
students(2) = "Antony"
Console.WriteLine("First student is {0} ", students(0))
Console.WriteLine("Second student is {0} ", students(1))
Console.WriteLine("Third student is {0} ", students(2))
Console.ReadKey()
End Sub
End Module
Step 3) Run the code by clicking the Start button from the toolbar. You will get the following window:
We have used the following code:
Explanation of Code:
This will only happen if you have a dynamic array. If you had declared a fixed-size dimensional array and it is full of array elements, you cannot add new elements to it. The following example demonstrates how to add new elements to a dynamic array:
Step 1) Begin by creating a new console application.
Step 2) Add the following code to the application:
Module Module1
Sub Main()
Dim nums() As Integer
ReDim nums(1)
nums(0) = 12
nums(1) = 23
For x = 0 To nums.Length - 1
Console.WriteLine("Initial array element: {0}", nums(x))
Next
ReDim Preserve nums(2)
nums(2) = 35
For x = 0 To nums.Length - 1
Console.WriteLine("Final array element: {0}", nums(x))
Next
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the toolbar to run the code. You should get the following window:
We have used the following code:
Explanation of Code:
It is recommended that you erase your array after you are done with it, mostly when you are dealing with a dynamic array. This will help you free up some memory space.
To delete an array, you just have to call the Erase statement followed by the name of the array. For example:
Dim nums(1) As Integer
nums(0) = 12
nums(1) = 23
Erase nums
We have declared an array of integers named nums and added two elements to it. The Erase nums statement will erase the array.
The split function provided by Visual Basic .NET helps us split a string into parts and keep them in an array. The following example demonstrates how to use this function:
Step 1) Create a new console application.
Step 2) Add the following code to the application:
Module Module1
Sub Main()
Dim myarray() As String
Dim gtupapers As String
Dim x As Integer
gtupapers = "Welcome, to, gtupapers"
myarray = Split(gtupapers, ", ")
For x = LBound(myarray) To UBound(myarray)
Console.WriteLine(myarray(x))
Next
Console.ReadKey()
End Sub
End Module
Step 3) Run the code by clicking the Start button from the toolbar. You should get the following window:
We have used the following code:
Explanation of Code:
The join function helps us join multiple arrays into a single string. The following example demonstrates this:
Step 1) Begin by creating a new console application.
Step 2) Add the following code to the application:
Module Module1
Sub Main()
Dim students(0 To 2) As String
students(0) = "John"
students(1) = "Alice"
students(2) = "Antony"
Dim classmates As String
classmates = Join(students, ", ")
Console.WriteLine(classmates)
Console.ReadKey()
End Sub
End Module
Step 3) Run the code by clicking the Start button on the toolbar:
We have used the following code:
Explanation of Code:
Programmers spend most of their days on a computer designing, writing, and testing code. This...
Here are computer science interview questions for fresher as well as experienced candidates to get...
Programmers spend a lot of time in front of PC and develop Repetitive Strain Injuries due to long...
Voice changer refers to software that can change the pitch or tone of a user's voice. It can be...
R is a programming language. To use R, we need to install an Integrated Development Environment...
Front End Development Tool is a software application which helps developers to build attractive...