VB.Net Array Basics

Discussion in 'Visual Basic [VB]' started by pradeep, May 6, 2007.

Tags:
  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    The ability to work with arrays is important in any programming language. VB.NET offers a simple way of grouping data into the array structures similarly to other languages. In this article, I will look at array declaration and usage.

    Purpose of the arrays



    Arrays are generally used for storing similar types of values or objects. They allow grouping variables together and allow referring to them by using an index. Arrays have an upper bound and a lower bound, which simply refer to the starting index and the ending index of a given array structure. Additionally, the data in the array can be sorted. Simple arrays are one-dimensional; however, you can also use multi-dimensional arrays in VB.NET. You can loop through an array to determine and to modify the values stored in the array.

    Declaring and initialising arrays



    There are two ways of initialising the arrays: to declare and initialise them in one statement, or to declare an array and choose to initialise it later.

    When declaring and initialising arrays in the same statement, you must specify the type of the array elements and the number of the elements the array will hold. In VB.NET, arrays are zero based, which means that the index of the first element is zero. The elements are numbered sequentially. You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the number that specifies the index of the last element in the array. Snippet A shows the declaration and initialisation of an array of integers.

    Snippet A
    Code:
    Dim arrNumbers(4) As Integer 'Declares & initialises an array of 5 integers, with indexes ranging from 0 to 4
    Another way to declare and initialise arrays is to perform these operations in two separate steps. If you declare an array without specifying a number of elements on one line, you have to provide the values for each item of the array when you initialise it. The initial values are provided enclosed in the {} braces, using a comma as a separator. Snippet B shows the declaration and initialisation of an array in two separate steps.

    Snippet B
    Code:
    Dim arrNumbers() As Integer 'Declares the array of integers
    arrNumbers = New Integer() {0,1,2,3,4} 'Initialises the array to five members & sets their values
    Once an array is declared and initialised, it's possible to change the size of an array in run time by redefining it. You can use the ReDim statement to change the number of items in an array structure. Snippet C shows declaration, initialisation, and then re-sizing of an array structure.

    Snippet C
    Code:
    Dim arrNumbers(32) As Integer' Declares & Initialises an array of integers
    ReDim arrNumbers(45) As Integer' Re-initialises the array
    By default, the data stored in an array is lost whenever an array is re-initialised. However, you can use the ReDim statement with the Preserve keyword in order to keep the existing data in the array when it's being re-initialised. Snippet D re-initialises an array structure using the Preserve statement to keep the data already stored in the array.

    Snippet D
    Code:
    Dim arrNumbers () As Integer = {0,1,2,3,4}' Declares & initialises the array
    ReDim Preserve arrNumbers (25) 'Resizes the array, but retains the data in elements 0 through 4
    There are two types of multidimensional arrays: rectangular or jagged. In rectangular arrays, every member of each dimension is extended into the other dimensions by the same length. In jagged arrays, individual members of one dimension can be extended into other dimensions by different lengths. The more dimensions an array has, the more complex it becomes to work with it.
     
  2. kiddo

    kiddo New Member

    Joined:
    Apr 11, 2009
    Messages:
    65
    Likes Received:
    1
    Trophy Points:
    0
    The keywords 'ReDim' & 'Preserve' are really helpful!
    Coz I just knew to use the Array in Snippet A & B.
    Thx
     
  3. kiddo

    kiddo New Member

    Joined:
    Apr 11, 2009
    Messages:
    65
    Likes Received:
    1
    Trophy Points:
    0
    in the snippet D,
    is it just 'ReDim Preserve arrNumbers (25)'?
    no '...As Integer'?
     
  4. chyssa

    chyssa New Member

    Joined:
    Oct 23, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for dharing your ideas to us...
     
  5. naqiali

    naqiali New Member

    Joined:
    May 4, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
     
  6. JohnRD

    JohnRD New Member

    Joined:
    Oct 9, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I would like to learn how to use arrays to store sentences, then use a label or a button to show each one.
     
  7. 0702R55795

    0702R55795 New Member

    Joined:
    Apr 13, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    similiar to this topic, my idea here is to take a string of characters from .dat file and store in a variable.
    When i initialise the array like this Dim array(42) As Integer = {abc} where abc is the variable that stores the string of characters from the .dat file. How can I read the file, do I use StreamReader or something else? Is my logic here correct or wrong. I am planing the flow out before I start this project. Thanks in advance though. Smile
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice