Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010

  • More Computers
  • More Games
  • More Electronics
  • More Books
  • More Software
  • Visual Basic Dynamic Array Introduction

    A dynamic array is an array that changes size while the program is running. Unlike the static array in the example above, dynamic arrays do not specify their size when they are declared. Unless you are certain the size of an array won't change, (like 7 days in a week), it's best to use dynamic arrays, so your program can take full benefit of any enhancements added to the system the program is running on. Also, dynamic arrays can shrink in size when your program doesn't need so much memory, allowing other applications to utilize the system's resources.

    Visual Basic Dynamic Array Declaration

    General Syntax

    (Dim, Private or Public) ArrayName() As DataType(i.e. int, double, or object)

    For example, the following statement declares a dynamic array to store an unknown number of names. The declaration informs the compiler that there is a dynamic array with a specified name in the program to store String data. No memory is allocated at declaration time:

    Dim EmployeeName () As String

    The Keyword Dim is used to declare a local array, Keyword Private is used to declare module-scope arrays, and Keyword Public is used to declare global-scope arrays.

    Buy from Centurion

    Visual Basic Resizing a Dynamic Array - Resetting Data

    At declaration time, no memory is allocated for a dynamic array. In order to store values in a dynamic array, the array must be resized to prepare the array to accept data. There are two ways to resize a Visual Basic dynamic array.

    ReDim: The keyword ReDim is used to resize a dynamic array to the desired size, while resetting the existing values in an array to their default values (0 for numeric arrays, and False for Boolean Arrays). An array can be resized as many times as necessary during the program execution to a bigger or smaller size. The general syntax for ReDim is:

    ReDim ArrayName(NewArraySize)

    Where ArrayName is the name of the Visual Basic dynamic array, and NewArraySize is a whole number greater than or equal to 0, specifying the size of the array. After ReDim, the dynamic array will have NewArraySize + 1 elements.

    Create the Form_Load method by double-clicking on the blank form as indicated in the diagram below:

    Create Form Load

    Figure Array-14: Create Form_Load Method

    Help Support this site - Click this ad

    Enter the code in the code window as indicated in the diagram below:

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Data() As Short
            ReDim Data(2)
            Dim I As Integer
            Data(0) = 10
            Data(1) = 11
            Data(2) = 12
            ReDim Data(3)
            Data(3) = 13
            I = I
        End Sub
    End Class

    Figure Array-15: Visual Basic Code to Create Dynamic Array

    Help Support this site - Click this ad

    Let's take a look at the code before we run the program:

    • Private Sub Form1_Load: This is the first access point, as the program begins to execute.
    • Dim Data() As Short: Declare a dynamic array of type short (Numeric values between -32767 to 32768)
    • ReDim Data(2): Give the Array of size 2 - and causing all elements to become 0.
    • Dim I As Integer: Dummy variable so we can enable the Debugger.
    • Data(0) = 10: Set element 0 to 10.
    • Data(1) = 11: Set element 1 to 11.
    • Data(2) = 12: Set element 2 to 12.
    • ReDim Data(3): Resize Array to be 4 elements long.
    • Data(3) = 13: Set element 3 to 13.
    • I = I: The debugger will start at this point before the line is executed.

    Set a breakpoint as indicated in the diagram below:

    Set Break Point

    Figure Array-16: Set a Visual Basic Breakpoint to observe array values

    Help Support this site - Click this ad

    Make sure to point to the column just left of the code window. This is the Set/Clear Breakpoint column. The RED dot indicates that a breakpoint has been set. When the code parser comes to the red dot program execution will be suspended while the program developer observes the variable status.

    Start the debugger by pressing the right triangle as indicated in the diagram below:

    Start Debugging

    Figure Array-17: Visual Basic Start Debugging

    Help Support this site - Click this ad

    The program should execute until the parser reaches our Red Dot in the Breakpoint Column. The Red Dot should have a yellow right arrow (→) pointing to the line the program is ready to execute. The line waiting to be executed will be highlighted as indicated in the diagram below:

    Pause At Breakpoint

    Figure Array-18: Visual Basic Program Execution Paused at Breakpoint

    In order to continue this guide and completing this debugging session, press the Button Below:

    Help Support this site - Click this ad


    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


    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


    Nullable Types

    Loop Constructs

    For Next Loop

    Do While Loop

    Do Until Loop



    Exit Do | Exit For


    Nested Loops


    Array Declaration

    Rnd( ) Function

    Listbox Control


    Parallel Arrays

    Key Event Args

    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger

    ReDim Preserve

    MultiDimensional Arrays

    DataGridView Control

    Length and Sort Methods


    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