Lesson 10: Creating Arrays in VB2019

Master data collections to efficiently manage groups of related items

Key Takeaway

Arrays are essential data structures that allow you to store and manage multiple values under a single name. Mastering arrays will significantly enhance your ability to handle collections of data efficiently in Visual Basic.

Arrays in Visual Basic 2019 are like containers that allow you to store multiple values of the same data type under a single name. Instead of declaring numerous individual variables, arrays provide a structured way to manage groups of related data, making your code more efficient and organized.

10.1 Array Dimensions

Arrays can have different dimensions depending on how you need to organize your data:

One-Dimensional Arrays

A simple list of items arranged in a single row or column. Think of it as a straight line of boxes where each box holds one value.

Item 0
Index 0
Item 1
Index 1
Item 2
Index 2
Item 3
Index 3
Table 10.1: One-Dimensional Array
Student Name Name(0) Name(1) Name(2) Name(3) Name(4) Name(5)

Two-Dimensional Arrays

A table of items arranged in rows and columns. This is useful for representing spreadsheet-like data or matrices.

Table 10.2: Two-Dimensional Array
Column1 Column2 Column3 Column4
Row1 Name(0,0) Name(0,1) Name(0,2) Name(0,3)
Row2 Name(1,0) Name(1,1) Name(1,2) Name(1,3)
Row3 Name(2,0) Name(2,1) Name(2,2) Name(2,3)
Row4 Name(3,0) Name(3,1) Name(3,2) Name(3,3)

Important Notes

In VB2019, arrays are zero-indexed, meaning the first element is at index 0. The length of an array is always one more than its highest index.

10.2 Declaring Arrays

In Visual Basic 2019, you declare arrays using the Dim statement, similar to regular variables but with the addition of parentheses to indicate dimensions.

10.2.1 One-Dimensional Arrays

The syntax for declaring a one-dimensional array:

Dim arrayName(n) As DataType

Where n represents the highest index in the array. The actual number of elements is n+1.

Form1.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Declare an array with 11 elements (index 0 to 10)
    Dim CusName(10) As String
    
    ' Display the array length
    MsgBox("Array length: " & CusName.Length)
End Sub

Output:

> Array length: 11
Array length output
Figure 10.1: Array length displayed in message box

Example 10.2: Populating an Array

This example collects customer names through an input box and displays them in a list box.

Form1.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim num As Integer
    Dim CusName(5) As String
    
    ' Collect 6 customer names
    For num = 0 To 5
        CusName(num) = InputBox("Enter the customer name", "Enter Name")
        ListBox1.Items.Add(CusName(num))
    Next
End Sub
Input box for array data
Figure 10.2: Input box for collecting array data
List box with array values
Figure 10.3: List box displaying array values

10.2.2 Two-Dimensional Arrays

The syntax for declaring a two-dimensional array:

Dim arrayName(m, n) As DataType

Where m and n represent the highest indices for rows and columns. The total elements are (m+1) × (n+1).

Form1.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Declare a 5x6 array (6 rows × 7 columns)
    Dim CusName(5,6) As String
    
    ' Display total elements
    MsgBox("Total elements: " & CusName.Length)
End Sub

Output:

> Total elements: 42
Array element count
Figure 10.4: Total elements in a two-dimensional array

Example 10.5: Sales Volume Tracker

This program tracks sales volume for four products over six months using a two-dimensional array.

Form1.vb
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
    Dim prod, mth As Integer ' prod: product, mth: month
    Dim saleVol(4, 6) As Integer
    Const j = 1
    
    ' Add table headers
    ListVolume.Items.Add(vbTab & "January" & vbTab & "February" & vbTab & "March" _
        & vbTab & "Apr" & vbTab & "May" & vbTab & "June")
    ListVolume.Items.Add(vbTab & "____________________________________________")
    
    ' Collect sales data
    For prod = 1 To 4
        For mth = 1 To 6
            saleVol(prod, mth) = InputBox("Enter the sale volume for product " & prod & 
                " month " & mth)
        Next mth
    Next prod
    
    ' Display sales data
    For i = 1 To 4
        ListVolume.Items.Add("Product " & i & vbTab & saleVol(i, j) & vbTab & saleVol(i, j + 1) & 
            vbTab & saleVol(i, j + 2) & vbTab & saleVol(i, j + 3) & 
            vbTab & saleVol(i, j + 4) & vbTab & saleVol(i, j + 5))
    Next i
End Sub
Sales volume output
Figure 10.5: Sales volume displayed in a list box

10.3 Dynamic Arrays

Dynamic arrays allow you to set the size of an array at runtime, which is useful when you don't know the exact number of elements needed during design time.

Declaration Syntax

To create a dynamic array:

Dim myArray() As DataType

Then resize it at runtime with:

ReDim myArray(n)

For two-dimensional arrays:

ReDim myArray(m, n)

Example 10.6: Dynamic Square Numbers

This program creates a dynamic array to store square numbers based on user input.

Form1.vb
Public Class Form1
    Private Sub BtnDisplay_Click(sender As Object, e As EventArgs) Handles BtnDisplay.Click
        Dim myArray() As Integer
        Dim i, n As Integer
        
        ' Get array size from user
        n = InputBox("Enter the upper bound of array")
        ListBox1.Items.Clear()
        
        For i = 1 To n
            ' Resize array for each element
            ReDim myArray(i)
            myArray(i) = i ^ 2
            ListBox1.Items.Add(myArray(i))
        Next
    End Sub
End Class
Dynamic array output
Figure 10.6: Dynamic array displaying square numbers

Lesson Summary

In this lesson, you've learned how to work with arrays in Visual Basic 2019:

Array Dimensions

Understand one-dimensional and multi-dimensional arrays

Declaration

Declare arrays with specific sizes and data types

Data Access

Access and manipulate array elements using indexes

Dynamic Arrays

Create arrays with sizes determined at runtime

Practical Applications

Implement arrays in real-world scenarios like data tracking

Arrays are fundamental data structures that will help you manage collections of data efficiently. In the next lesson, we'll explore mathematical operations and how to perform calculations with arrays.

Next Lesson

Ready to learn about mathematical operations? Continue to Lesson 11: Math Operations.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon