How To Create An Array In Visual Basic
To create an array in Visual Basic .NET, declare it with Dim, give it a name and element type, and specify its highest index or supply initial values. For example, Dim numbers(4) As Integer creates five elements, indexed from 0 to 4. Use parentheses to.
Which licences fit your PC setup?
Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.
-

Office 2021 Pro Plus key
$57.00 AUD
-

SQL Server 2022 Standard
$399.99 AUD
-

Windows 11 Pro key
$14.99 AUD
$29.00
Create your first integer array
An array groups related values under one name. Instead of declaring a separate variable for each score, quantity or measurement, you can store the values in an array and identify each one by its position.
These examples use VB.NET. VBA and classic Visual Basic have different rules, particularly for array bounds, so keep your project's language in mind when adapting examples.
- Open a Visual Basic project and place the following statements inside a procedure, such as Main in a console application.
- Declare five integer elements with Dim numbers(4) As Integer. The number in parentheses is the upper bound, meaning the highest valid index.
- Set the first element with numbers(0) = 10 and the last with numbers(4) = 50. The other three elements remain zero.
- Read the first value with Dim firstNumber As Integer = numbers(0). In a console application, Console.WriteLine(firstNumber) displays 10.
The distinction between an upper bound and a count prevents a common mistake: Dim numbers(5) As Integer creates six elements. For five elements, use an upper bound of 4. Microsoft's Visual Basic array guide explains this indexing rule.
Initialise an array with known values
If you already know the contents, supply an initialiser in curly braces. This creates the array and assigns its values in one statement:
Dim names() As String = {"John", "Jane", "David"}
This array has three elements: names(0) contains "John", names(1) contains "Jane", and names(2) contains "David". You do not need to calculate an upper bound because the compiler determines the length from the initialiser.
The same approach works for numbers:
Dim scores() As Integer = {12, 18, 25}
If you specify both bounds and initial values, the values must fill the declared array. Leaving the bounds blank is usually clearer when the initialiser already establishes the count. See Microsoft's array initialisation examples for the supported forms.
Understand default values
Creating an array with a size but no initialiser gives each element its type's default value. Integer elements start at 0, Boolean elements at False, and String elements at Nothing. A String element is not automatically initialised to an empty string.
By contrast, Dim names() As String declares an array variable without creating an array. Its value is Nothing until you assign or allocate one. Accessing names.Length or names(0) before that will fail. Microsoft's Dim statement reference covers declarations and default values.
Read, update and loop through elements
VB.NET uses parentheses for array access. To change the second score, write scores(1) = 20. To store that score in another variable, write Dim secondScore As Integer = scores(1).
For a one-dimensional array, valid indexes run from 0 to Length minus one. With three scores, scores.Length is 3, but scores(3) is outside the array and causes an IndexOutOfRangeException.
Use a For loop when you need the index
To display each score, enter these statements on separate lines inside your procedure:
- Start the loop with For i As Integer = 0 To scores.Length - 1.
- Inside the loop, add Console.WriteLine(scores(i)).
- Close the loop with Next.
You can replace the display statement with an assignment, such as scores(i) = scores(i) + 1, to update each element. The end value in a For...Next loop is inclusive, which is why the upper limit subtracts one from Length.
When you only need each value, use For Each score As Integer In scores, followed by Console.WriteLine(score), then Next, each on its own line. To replace stored integer values, use the indexed loop rather than assigning to the For Each loop variable.
Allocate or resize an array with ReDim
If the required count becomes known later, declare Dim values() As Integer, then allocate ten elements inside a procedure with ReDim values(9). Allocate the array before attempting to access its elements.
To expand it to fifteen elements while keeping existing values, use:
ReDim Preserve values(14)
The original ten values remain, and the five new integer elements start at zero. ReDim values(14), without Preserve, replaces the contents with default values instead.
Shrinking with Preserve keeps only the elements that fit; discarded elements are lost. For multidimensional arrays, Preserve permits resizing only the last dimension. These behaviours are documented in Microsoft's ReDim reference.
Create a two-dimensional array
Use two indexes when your data naturally forms a grid. The declaration Dim matrix(3, 4) As Integer creates four rows and five columns, because each dimension starts at zero.
Write matrix(1, 2) = 10 to set the element in the second row and third column. Both indexes must stay within their respective bounds.
For this array, matrix.Length is 20, the total number of elements. matrix.Rank is 2, the number of dimensions. Use matrix.GetLength(0) for the row count and matrix.GetLength(1) for the column count when building nested loops.
Practise with a small form
Apply the examples to a form that stores several entered scores. Use the Visual Basic tutorials for further exercises. As you add controls, the guides to coding a clear button and coding a checkbox provide related tasks for handling user input.
Start by creating the three-element scores array, changing scores(1), and displaying every value with a loop. Check that only the second value changes before connecting the array to your form.
Other editions for separate workloads
Other editions for separate workloads; prices in AUD, confirm the total on the listing.
-

Office 2024 Pro Plus key
$20.00 AUD
$39.00 -

Windows 11 Home key
$18.88 AUD
$37.00 -

Windows 10 Pro key
$12.00 AUD
$15.00
How to check this worked
- Open Settings, System, Activation and confirm the edition shown.
- Enter the key and run activation; expect a green tick within a minute.
- Reboot and re-check activation survives the restart.
FAQ
What else should you know before buying?
- What do I need before starting?
- Read the requirements and confirm the edition first; the steps assume the machine is compatible.
- How do I know the licence is genuine?
- Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
- Is this a subscription?
- Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
- What if the key fails?
- Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.
Check who prepared and reviewed this guide
- Written by
- MS.Codes editorial
- Reviewed by
- Support team
Last verified: (source review; see test status above).