Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010


 
MoreDesktop.php
  • More Computers
  •  
    Morexbox.php
  • More Games
  •  
    Moreaccessories.php
  • More Electronics
  •  
    MoreMobileWirelessBooks.php
  • More Books
  •  
    MoreMultimedia.php
  • More Software
  • Visual Basic.NET Arrays

    Introduction

    Without using arrays, it is still possible to perform computations, process numeric data and so on. In some applications, one has to collect the data values, store them in the program, and be able to access each value later on. For example, consider an application designed for book keeping in a bookstore. It requires entering the name, ISBN, author's name and other data for each book, one at a time. The entered data should be stored in the computer's memory for certain processing required by the office. Without using arrays, you can write a program to get the data for one book, store it in variables and do some processing. But once the next book's data is entered, it will be stored in the same variables, hence erasing the previously stored data. What is the solution? Is it possible to declare enough variables to store the data for all the books in the store? That would be similar to the old-fashioned paper-based bookkeeping; besides it would be practically impossible. Luckily, programming languages have a data structure called arrays, which provides a solution for this kind of problem. By using arrays, many data values of the same type can be stored in the computer's memory under one single name. In other words, an array can be thought of as a variable that can store many data values of the same data type, instead of just a single value. Below are examples of arrays of different objects:

    27 31 88 16 93

    Figure Array-1: Array of Numbers


    Jack Jill hill tumble crown

    Figure Array-2: Array of Words


    Buy from Centurion


    Definition

    An array is a data structure that is used to store data values of the same data type in the computer's memory under one name. Like a variable, to use an array in a Visual Basic program, the array must be declared before it is used.

    Declaration of One-Dimensional Array

    At declaration time, the name of the array, data type of the values that can be stored in it, and its size should be specified. To name an array, follow the same naming rules and standards as those for variables. Declaration syntax for VB.NET is as follows:

    Dim|Private|Public ArrayName(LastIndex) As DataType

    • Dim | Private | Public:
      • Dim: Example: Dim Array1(50). The Dim keyword is used when declaring an array with local scope.

      • Private: Example: Private Array2(50). The Private keyword is used when declaring an array with module scope.

      • Public: Example: Public Array3(50). The Public keyword is used when declaring an array with Project scope.

    • ArrayName: This is the name given to the array.

    • LastIndex: This must be a nonnegative integer value. This is the maximum number of elements you expect to store in the array.

    • DataType: This is the data type of the values stored in the array. i.e. Integer or Double.

    Help Support this site - Click this ad

    Array Index

    At declaration time, enough memory is allocated to store values of the specified data type. Each of the allocated memory locations is referenced by a nonnegative Index value for that element. The Index of the first element in an array in Visual Basic.NET is always 0. The elements are accessed sequentially, as can be seen in the following examples:

    Example 1

    Declare an array to store the area codes of ten cities in Alaska. Do they have ten cities in Alaska? Assume that the area codes are three-digit whole numbers, i.e. (0, 1, 2, 3... 9):

    Dim AreaCode(10) As Integer

    With the above declaration, ten consecutive memory locations of two bytes each will be allocated in the computer's memory, and the array indexes proceed from 0 to 9, with the first element being 0, and the last element being 9. The following is a represenation of how the declaration Dim AreaCode(10) would appear in the computer's memory:

    Area Code
    Index 0 1 2 3 4 5 6 7 8 9
    Value 0 0 0 0 0 0 0 0 0 0

    Notice that value of all the elements in an array in Visual Basic.NET are initialized to 0 when the array is decalred.

    Accessing Array Elements

    Each array element can be used like a single variable of the same data type. To access an array element, state the name of the array with the element you want to access in parentheses. For example: AreaCode(0) will access the first element in the array described in Example 1 above.

    Help Support this site - Click this ad

    Example 2

    Declare an array to store the prices of 5 video games.

    Dim Price(5) as float

    With the above declaration, five consecutive memory locations of four bytes each will be allocated in the computer's memory, and the array indexes proceed from 0 to 4, with the first element being 0, and the last element being 4. The following is a represenation of how the declaration Dim Price(5) would appear in the computer's memory:

    Price
    Index 0 1 2 3 4
    Value 0 0 0 0 0

    Perform the following operations on this array:

    1. Store 12.50 in the first element of the Price array.
      • Price(0)=12.5
      • The array in memory would now appear as:


      Price
      Index 0 1 2 3 4
      Value 12.50 0.00 0.00 0.00 0.00

    2. Store 89.95 at index 2 of the Price array.
      • Price(2)=89.95
      • The array in memory would now appear as:

      Price
      Index 0 1 2 3 4
      Value 12.50 0.00 89.95 0.00 0.00

    3. Store 19.95 at index j given that j=3.
      • Dim j As Integer=3
      • Price(j)=19.99
      • The array in memory would now appear as:

      Price
      Index 0 1 2 3 4
      Value 12.50 0.00 89.95 19.99 0.00

    4. Store 36 at the index 1 greater than j
      • Price(j+1)=36
      • The array in memory would now appear as:

      Price
      Index 0 1 2 3 4
      Value 12.50 0.00 89.95 19.99 36.00

    5. Store the sum of values in index locations 3 and 4 in index location 1.
      • Price(1)=Price(3)+Price(4)
      • The array in memory would now appear as:

      Price
      Index 0 1 2 3 4
      Value 12.50 55.99 89.95 19.99 36.00

    Help Support this site - Click this ad

    Subscript Out of Range Error:

    An attempt to access a nonexisting index in an array, i.e. Price(6), results in a Subscript out of Range error and the program execution halts.

    Working with One-Dimensional Arrays

    Arrays and loops go hand in hand. When working with arrays, one often needs to display the entire array, find the maximum or minimum value stored int the array, search for a certain value in the array, and so on. In such cases, a loop structure is needed.

    Example 3

    Perform the following operations in sequence:

    1. Declare an array to store the test scores of 25 students.
      • Const ArraySize As Integer=25
      • Dim Score(ArraySize) As Integer

    2. Fill the array with random test scores between 0 and 100.
      • Dim Index As Integer
      • For Index = 0 to cSize
        • Score(Index)=Rnd( )*100
      • Next Index

    3. Assume that array Score is filled with random numbers. Display all the scores in the output Label.
      • Dim Index As Integer
      • For Index=0 to cSize
        • lblShow.Text=lblShow.Text & Score(Index).ToString & vbcrlf
      • Next Index
    Help Support this site - Click this ad

    Create the Form by following the steps below:

    1. Select the Form1.vb[Design] tab.
    2. Add the label control to your form.
    3. Place it in the upper right corner.
    4. Extend the length of the form to allow the display of 25 lines (450-500 pixel length).

    Example 3 Form

    Figure Array-3: Form Design for example 3


    In order to continue this guide and finish Example #3 where we fill an Array with random values press the Button Below:


    Help Support this site - Click this ad

    Home


    Visual Basic

    Introduction to VB.NET

    .NET Framework

    VS2008 IDE

    How VB is Compiled

    Start Visual Studio


    Windows Form App


    Save Your Work

    VB OOP Programming

    Visual Basic Code


    Exit Code


    Button Event Code

    Coding Recommendations

    If/Then/Else

    Error List Window

    Comment Syntax

    Help Window

    Language Essentianl

    Built-In Data Types


    Declare Variables

    Declare Constants

    Code Arithmetic Expressions

    Assignment Statements

    Operator Precedence

    Type Casting


    Math Class

    String Declaration

    Conversion Functions

    Conversion Methods

    Formatting Functions

    String Formatting


    Variable Scope

    Enumerations

    Nullable Types


    Loop Constructs

    For Next Loop

    Do While Loop


    Do Until Loop

    Do...Loop-While

    Do...Loop-Until


    Exit Do | Exit For

    Do...Loop


    Nested Loops


    Arrays

    Array Declaration


    Rnd( ) Function

    Listbox Control


    KeyPressEventArgs


    Parallel Arrays

    Key Event Args


    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger


    ReDim Preserve


    MultiDimensional Arrays

    DataGridView Control


    Length and Sort Methods


    Structures

    Pad Right

    Split Method

    IsNumeric Function


    Multiform Projects


    Add Form To Project

    Form Object Methods

    Form Show Method

    ShowDialog Method

    Form Close Method

    Form Accept Button

    Multiform Project Example


    ASP.NET Web Programming

    Create Data Source


    Configure Access Data Source


    Add Product Class


    Extract Local Database Data

    Order PageLoad VB Code

    Add New Web Page


    Set Start Page

    Display Cart Aspx Code

    Display Cart Design View

    Sorted List Definition

    VB.NET Session State


    Create CartItem Class

    GetCartContents Function


    Add To Cart Event Handler

    Remove Cart Item Event


    Clear Cart Event Handler