VB.NET Array Examples

VB.NET Array

Array: conceptual illustration

The VB.NET language offers array types. Arrays are used as low-level representations of collections of elements. They often provide the best performance for certain tasks. Other collections are implemented with arrays internally.

An array is a fixed collection of same-type data that are stored contiguously and that are accessible by an index. Sedgewick, p. 83

Example

There are several different syntax forms you can use to declare one-dimensional arrays in the VB.NET language. Let's get started by looking at the following code, which creates an array by specifying the maximum index in the first statement. Please note that in VB.NET, you need to specify the maximum index—not the actual array length.

Program that uses array [VB.NET]

Module Module1
    Sub Main()
	' Create an array.
	Dim array(2) As Integer
	array(0) = 100
	array(1) = 10
	array(2) = 1

	For Each element As Integer In array
	    Console.WriteLine(element)
	Next
    End Sub
End Module

Output

100
10
1

Integer array 2

Often you can initialize your VB.NET array in a simpler way than in the previous example. Here, we use the curly-bracket { } syntax to create an array of three elements. Note that you do not need to declare a maximum index on the array.

Program that uses another array syntax form [VB.NET]

Module Module1
    Sub Main()
	' Create array of three integers.
	Dim array() As Integer = {10, 30, 50}

	For Each element As Integer In array
	    Console.WriteLine(element)
	Next
    End Sub
End Module

Output

10
30
50

Integer array 3

In addition to the For Each looping style as shown above, you can use the For loop construct. This allows you to access the index of each element, which can be useful for additional computations or logic in your loop.

Length
Program that uses For loop on array [VB.NET]

Module Module1
    Sub Main()
	' Create array of three integers.
	Dim array() As Integer = {100, 300, 500}

	' Use for loop.
	For i As Integer = 0 To array.Length - 1
	    Console.WriteLine(array(i))
	Next
    End Sub
End Module

Output

100
300
500

String array example

String type

Arrays are not just for values like Integers. They are also useful for reference types like Strings. We create a four-element String array and place four String references inside of it. One interesting thing is that the strings are actually not stored in the array. Instead, reference values are stored in the array and the objects are stored on the managed heap.

Program that uses String array [VB.NET]

Module Module1
    Sub Main()
	' Create array of maximum index 3.
	Dim array(3) As String
	array(0) = "dot"
	array(1) = "net"
	array(2) = "perls"
	array(3) = CStr(2010)

	' Display.
	For Each element As String In array
	    Console.WriteLine(element)
	Next
    End Sub
End Module

Output

dot
net
perls
2010

Array argument example

Let's say you need to pass an array to a subroutine or function in your VB.NET program. Fortunately, this example shows you exactly how to do that. Please note that the entire array is not copied. Rather, just the reference to the array is copied as the new function is called. This is much faster.

Program that passes array as argument [VB.NET]

Module Module1
    Sub Main()
	Dim array() As Integer = {5, 10, 20}
	' Pass array as argument.
	Console.WriteLine(Example(array))
    End Sub

    ''' <summary>
    ''' Receive array parameter.
    ''' </summary>
    Function Example(ByVal array() As Integer) As Integer
	Return array(0) + 10
    End Function
End Module

Output

15

Return array

How can you have a function in your VB.NET program return an array? This program demonstrates the correct syntax. The Example() function creates a two-element array and then returns it. The Main() subroutine then displays all its results by calling String.Join on them.

Program that returns array [VB.NET]

Module Module1
    Sub Main()
	Console.WriteLine(String.Join(",", Example()))
    End Sub

    ''' <summary>
    ''' Return array.
    ''' </summary>
    Function Example() As String()
	Dim array(1) As String
	array(0) = "Perl"
	array(1) = "Python"
	Return array
    End Function
End Module

Output

Perl,Python

Resize array

The VB.NET programming language

You can resize arrays with the Array.Resize subroutine. If you have to resize arrays frequently, it is better to use the List type, which resizes automatically.

Array.Resize Subroutine List Tips

Complex arrays

Two-dimensional (2D)

Arrays are not limited to one dimension. You can use two-dimensional arrays—arrays that are square or rectangular—or even jagged arrays, which are basically arrays of arrays. Thus each array element is actually a whole separate array. Multidimensional arrays beyond two dimensions are also possible.

2D Array Jagged Array

Reverse array

You don't need to write code to reverse the ordering of elements in your array. The Array.Reverse subroutine is both faster and simpler to use in most cases.

Array.Reverse

Specific types

Note

You must specify a type for every array element. In the VB.NET language, Char arrays are useful for character-level manipulations, while an Object array is useful for storing derived types with base references.

Char Array Object Array

Misc.

Miscellaneous

The VB.NET language offers a whole extra layer of functionality, including a lot of global functions that have interesting effects. One example is the Choose function, which returns a specific element in your array.

Choose Function Example

Summary

Arrays are an important type in the VB.NET language. They are used inside other types such as List and Dictionary to implement those types' storage. They have a lot of performance advantages as well, making them a critical part of your programming skill set.

.NET