In programming, there are many cases that we need to reuse or we want to store many values in a single variable, but the problem is that variables can only hold one value. On the other hand we will be using an array. An array is a series of objects, with the same type and size. Each object in an array is called an element of an array. It's either you have an array of strings or characters, integers or any that has defined data type.
The main use of an array is to organize homogeneous data together as a group to perform operations on data in a collective manner since traversal and retrieval become easy.
This time, we’re going to create a new windows form application project and save it as “Arrays”. Then add a button and add the following code:
Take note, arrays is accessed by their index number and an array always starts from zero.
'declare array as a string with six index
Dim words(6) As String
'here, we assign values to specific index of an array
words(0) = "Visual"
words(1) = "Basic"
words(2) = "programming"
words(3) = "is"
words(4) = "fun"
words(5) = "to"
words(6) = "learn!"
'display a value based on the specific index
Next, we’re going to display a specific value based on the user input. To do this add a textbox to your form. Then modify your code and it will look like as shown below.
'declare array as a string with six index
Dim words(6) As String
'here, we assign values to specific index of an array
words(0) = "Visual"
words(1) = "Basic"
words(2) = "programming"
words(3) = "is"
words(4) = "fun"
words(5) = "to"
words(6) = "learn!"
'declare new variable as integer
Dim i As Integer
'assign value from textbox1 to a variable
i = TextBox1.Text
'display a value based on the specific index
Then you can now test your application by pressing “F5”.