Lesson 10

Creating Arrays

In the previous lesson, you learned how to store individual values using variables and constants. In this lesson, you will learn how to work with a group of related values by using arrays. Arrays are extremely useful when you need to store many items of the same data type, such as a list of names, marks, prices, or scores.

Lesson focus:

Instead of declaring many separate variables such as Name1, Name2, Name3, and so on, you can use one array name and distinguish each item by its position, also called an index or subscript.

Lesson Overview

Lesson10
TopicArrays
Main FocusOne-Dimensional and Two-Dimensional Arrays
Key SkillDeclaration and Indexing
Next StepMath Operations
10.1 What Is an Array?
10.2 Array Dimensions
10.3 Declaring Arrays
10.4 Practical Examples

What Is an Array?

A group of variables of the same data type is known as an array in Visual Basic 2015. If you only need to store a single value, one variable is enough. However, if you need to store many similar items, using separate variable names becomes tedious and hard to manage.

For example, if you want to store 100 customer names, it would be inconvenient to declare 100 different variables. Instead, you can declare one array and then refer to each item by its position:

Name(0), Name(1), Name(2), Name(3) ...

Each position number is called an index or subscript.

One-Dimensional and Two-Dimensional Arrays

In Visual Basic 2015, an array can be one-dimensional or multidimensional. Most simple programs use one-dimensional or two-dimensional arrays.

A one-dimensional array is like a list of items arranged in one row or one column. A two-dimensional array is like a table made up of rows and columns.

To reference an element in a one-dimensional array, use:

ArrayName(x)

To reference an element in a two-dimensional array, use:

ArrayName(x, y)
Table 10.1: One-Dimensional Array
Student Name Name(0) Name(1) Name(2) Name(3) Name(4) Name(5) Name(6)
Table 10.2: Two-Dimensional Array
Name(0,0) Name(0,1) Name(0,2) Name(0,3)
Name(1,0) Name(1,1) Name(1,2) Name(1,3)
Name(2,0) Name(2,1) Name(2,2) Name(2,3)
Name(3,0) Name(3,1) Name(3,2) Name(3,3)

Declaring a One-Dimensional Array

In Visual Basic 2015, you can use either the Dim statement or the Public statement to declare an array.

  • Dim creates an array that is local to a procedure or module.
  • Public creates an array that can be used throughout the application.

The syntax for a one-dimensional array is:

Dim arrayName(n) As dataType

Here, n indicates the last index in the array, not the number of elements. Since Visual Basic arrays normally start at index 0, an array declared as (10) actually contains 11 elements, from index 0 through index 10.

Dim CusName(10) As String

The array above contains 11 string elements:

CusName(0) ... CusName(10)

To find the total number of elements in an array, use:

arrayName.Length

Finding the Length of a One-Dimensional Array

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim CusName(10) As String
    MsgBox(CusName.Length)

End Sub

When the program runs, the message box displays the length of the array, which is 11.

VB2015 Figure 10.1 Length of one-dimensional array

Figure 10.1: The length of the array is displayed as 11

Declaring an Array with Initialized Values

You can also declare an array and initialize it with values in a more flexible way:

Dim arrayName As DataType()
arrayName = New String() {1, 2, 3, ...., n}

This approach creates the array with the values you provide immediately.

Array with Three Initialized Values

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim CusName As String()
    CusName = New String() {1, 2, 3}
    MsgBox(CusName.Length)

End Sub

In this example, the length of the array is 3 because the array contains three elements.

Declaring a Two-Dimensional Array

The syntax for declaring a two-dimensional array is:

Dim ArrayName(m, n) As dataType

Here, m and n are the last indices in the two dimensions. The total number of elements is:

(m + 1) × (n + 1)

For example, if you declare:

Dim CusName(5, 6) As String

then the total number of elements is:

(5 + 1) × (6 + 1) = 42

Finding the Length of a Two-Dimensional Array

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim CusName(5,6) As String
    MsgBox(CusName.Length)

End Sub

When the program runs, the message box displays the length of the array as 42.

VB2015 Figure 10.2 Length of two-dimensional array

Figure 10.2: The length of the two-dimensional array is displayed as 42

Storing Multiple Names in an Array and Displaying Them in a ListBox

The following example shows how to collect six customer names from the user and store them in an array. Each name is entered through an InputBox and then added to a ListBox.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim num As Integer
    Dim CusName(5) As String

    For num = 0 To 5
        CusName(num) = InputBox("Enter the customer name", "Enter Name")
        ListBox1.Items.Add(CusName(num))
    Next
End Sub

This example combines several ideas you have learned earlier:

  • InputBox collects user input.
  • CusName(num) stores each entered name in the array.
  • For...Next repeats the input process six times.
  • ListBox1.Items.Add(...) displays the names in the list box.
VB2015 Figure 10.3 Input box prompting for customer name

Figure 10.3: Entering customer names through an InputBox

VB2015 Figure 10.4 ListBox showing entered names

Figure 10.4: The customer names displayed in the ListBox

Why Arrays Are Useful

Core takeaway:

Arrays let you store and manage many related values efficiently under one name. This makes your code cleaner, shorter, and easier to maintain. As your programs become larger, arrays become essential tools for handling repeated or grouped data.

Build on This Foundation

Continue to VB2026

After learning the basics of arrays in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the difference between a one-dimensional array and a two-dimensional array?
  2. If you declare Dim Score(4) As Integer, how many elements does the array contain?
  3. Write a short VB2015 example that stores five names in an array and shows them in a ListBox.

Go to Lesson 11

In the next lesson, you will learn how to perform math operations in Visual Basic 2015.