Arrays in VB

download Arrays in VB

of 3

Transcript of Arrays in VB

  • 8/8/2019 Arrays in VB

    1/3

    Arrays

    Arrays are programming constructs that store data and allow us to access them by numericindex or subscript. Arrays helps us create shorter and simpler code in many situations.Arrays in Visual Basic .NET inherit from the Array class in the System namespace. All arraysin VB are zero based, meaning, the index of the first element is zero and they are numberedsequentially. You must specify the number of array elements by indicating the upper boundof the array. The upper bound is the numder that specifies the index of the last element ofthe array. Arrays are declared using Dim, ReDim, Static, Private, Public and Protectedkeywords. An array can have one dimension (liinear arrays) or more than one(multidimensional arrays). The dimensionality of an array refers to the number of subscriptsused to identify an individual element. In Visual Basic we can specify up to 32 dimensions.Arrays do not have fixed size in Visual Basic.

    The following code demonstrates arrays.

    Imports System.ConsoleModule Module1

    Sub Main()Dim sport(5) As String'declaring an arraysport(0) = "Soccer"sport(1) = "Cricket"sport(2) = "Rugby"sport(3) = "Aussie Rules"sport(4) = "BasketBall"sport(5) = "Hockey"'storing values in the arrayWriteLine("Name of the Sport in the third location" & " " & sport(2))

    'displaying value from arrayEnd Sub

    End Module

    Understanding the Code

    The above code declared a sport array of type string like this: Dim sport(5) as String.This sport array has 6 elements starting from sport(0) to sport(5). The first element of anarray is always referred by zero index. The image below displays output from above code.

  • 8/8/2019 Arrays in VB

    2/3

    You can also declare an array without specifying the number of elements on one line, youmust provide values for each element when initializing the array. The following linesdemonstrate that:

    Dim Test() as Integer'declaring a Test arrayTest=New Integer(){1,3,5,7,9,}

    Reinitializing Arrays

    We can change the size of an array after creating them. The ReDim statement assigns acompletely new array object to the specified array variable. You use ReDim statement tochange the number of elements in an array. The following lines of code demonstratethat. This code reinitializes the Test array declared above.

    Dim Test(10) as IntegerReDim Test(25) as Integer'Reinitializing the array

    When using the Redim statement all the data contained in the array is lost. If you want topreserve existing data when reinitializing an array then you should use the Preservekeyword which looks like this:

    Dim Test() as Integer={1,3,5}'declares an array an initializes it with three membersReDim Preserve Test(25)'resizes the array and retains the the data in elements 0 to 2

    Multidimensional Arrays

  • 8/8/2019 Arrays in VB

    3/3

    All arrays which were mentioned above are one dimensional or linear arrays. There are twokinds of multidimensional arrays supported by the .NET framework: Rectangular arrays andJagged arrays.

    Rectangular arrays

    Rectangular arrays are arrays in which each member of each dimension is extended in eachother dimension by the same length. We declare a rectangular array by specifying additionaldimensions at declaration. The following lines of code demonstrate the declaration of amultidimensional array.

    Dim rectArray(4, 2) As Integer'declares an array of 5 by 3 members which is a 15 member arrayDim rectArray(,) As Integer = {{1, 2, 3}, {12, 13, 14}, {11, 10, 9}}'setting initial values

    Jagged Arrays

    Another type of multidimensional array, Jagged Array, is an array of arrays in which thelength of each array can differ. Example where this array can be used is to create a table inwhich the number of columns differ in each row. Say, if row1 has 3 columns, row2 has 3columns then row3 can have 4 columns, row4 can have 5 columns and so on. The followingcode demonstrates jagged arrays.

    Dim colors(2)() as String'declaring an array of 3 arrayscolors(0)=New String(){"Red","blue","Green"}initializing the first array to 3 members and setting valuescolors(1)=New String(){"Yellow","Purple","Green","Violet"}initializing the second array to 4 members and setting values

    colors(2)=New String(){"Red","Black","White","Grey","Aqua"}initializing the third array to 5 members and setting values