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.
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.
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.
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:

Example 10.2: Populating an Array
This example collects customer names through an input box and displays them in a list box.
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


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).
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:

Example 10.5: Sales Volume Tracker
This program tracks sales volume for four products over six months using a two-dimensional array.
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

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.
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

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

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.