Array Function in VB.NET

Greating everyones, this time I will discuss about Array, what make Array so special, it's ability to store multiple value, other variable can only store one value



Declaring One Dimensional Array

Dim arrValuesOne(9) As  Integer         'Create 10 slot with index start from 0 to 9

Dim arrValuesTwo(0 to 9) As Integer     'Also create 10 slot with index from 0 to 9


both code result same thing, to set the value of an array, refer to this code

arrValuesOne(0) = 1

arrValuesOne(1) = 2

arrValuesOne(2) = 3

arrValuesOne(3) = 4

arrValuesOne(4) = 5

arrValuesOne(5) = 6

arrValuesOne(6) = 7

arrValuesOne(7) = 8

arrValuesOne(8) = 9

arrValuesOne(9) = 10


This is the simpliest way declare and set an array value

Declaring Multi Dimensional Array

To create multi dimensional array, use the comma sign
Dim arrValues(0 to 9, 0 to 9) As Decimal


To set the value you can simply write it like this
arrValues(0, 0) = 100

arrValues(1, 1) = 200

arrValues(2, 2) = 300


and soon ...

We can give in array as many as 32 dimensional, but in reality we only use 2 or 3

Declaring Array Without Initialized

If we declare it like this
Dim arrValues() As Integer

this means we declare an array without giving the slot, later if we want to give a slot, we can simply write it like this

Redim arrValues(0 to 10)

arrValues(1) = 1000


To redim an array that already fill with value, and we don't want to reset the value, we can simply add Preserve keyworad infront of Redim

Redim Preserve arrValues(0 to 10)




Let's jump out to a sample code



First create a sample form like this


In this form the user will input any kind of string and everytime the user hit the Add button the string value will be saved into the array

And if the user hit the Remove button the string value that already save into the array will be removed one by one according to the order

Public Class Form1
    ' The array of strings.
    Private Values(0 To -1) As String
 
    ' Add a string to the array.
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        ' Get the old upper bound.
        Dim old_upper_bound As Integer = Values.Length - 1
 
        ' Make room for the new item.
        ReDim Preserve Values(0 To old_upper_bound + 1)
 
        ' Insert the new item.
        Values(old_upper_bound + 1) = txtString.Text
 
        ' Clear the TextBox.
        txtString.Clear()
        txtString.Focus()
    End Sub
 
    ' Remove a string from the array.
    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        ' Get the old upper bound.
        Dim old_upper_bound As Integer = Values.Length - 1
 
        ' Make sure the array isn't empty.
        If old_upper_bound < 0 Then
            ' There are no more items. Say so.
            txtString.Text = ""
        Else
            ' Display the last item in the array.
            txtString.Text = Values(old_upper_bound)
 
            ' Resize the array to remove the last item.
            ReDim Preserve Values(0 To old_upper_bound - 1)
        End If
    End Sub
End Class

Run the project, and try to add some item, after add several item, put a breakpoint in line 17, and hover the mouse over Values in line 14, you will see there are 4 item inside the Values array.

Another great way to create an array and this is my favorite, after defining the array name, use curly bracketto fill the value

Dim TheValues As Integer = {10,23,350,324,500}

Of course you must give a value that correspond with the data type, otherwise it will be crashed.

Share this

Related Posts

Latest
Previous
Next Post »