VB.NET Array

Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows

Array type

Arrays are low-level collections of elements. One element is stored after another in a linear way. Arrays often provide the best performance for certain requirements. Other collections are implemented with internal arrays.

Example

Program

There are many syntax forms we use to declare one-dimensional arrays in the VB.NET language. To start, this example creates an array by specifying in the maximum index in the first statement.

Note:In VB.NET we must specify the maximum index, not the actual array element count or length.

Based on:

.NET 4.5

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

Steps

Often you can initialize an 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

Program

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. This can be useful for additional computations or logic in your loop.

Length

Also:Using the For-loop syntax is better for when you need to modify elements within the array.

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

String type

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

String Array
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

We can pass an array to a subroutine or function. 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

Return keyword

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.

Then:The Main subroutine displays all its results by calling String.Join on them.

Join
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

First, last

First

How can we get the first and last elements in an array? We get the first element with the index 0.
For the last element,
we take the array Length
and subtract one. This works on all non-empty, non-Nothing arrays.

Caution:Arrays that are Nothing will cause a NullReferenceException to occur if you access elements on them.

NothingTip

Also:An empty array (one with zero elements) has no first or last element, so this code also will fail.

In the example, we use the ToCharArray Function to convert a String "abc" into an array of three Chars. This Function is useful in many program contexts. It gives us a mutable version of any String.

ToCharArray
Program that gets first, last elements: VB.NET

Module Module1
    Sub Main()
	' Get char array of three letters.
	Dim values() As Char = "abc".ToCharArray()

	' Get first and last chars.
	Dim first As Char = values(0)
	Dim last As Char = values(values.Length - 1)

	' Display results.
	Console.WriteLine(first)
	Console.WriteLine(last)
    End Sub
End Module

Output

a
c

Subs

Sub keyword

Many subroutines are available that act upon arrays. We search arrays with Find. We can copy and resize arrays with helpful subs. The Array.Reverse subroutine reverses the ordering of elements.

Array.CopyArray.FindArray.IndexOfArray.ResizeArray.Reverse

Tip:With arrays, using built-in subs is usually best. Writing custom methods should be avoided unless needed.

Types

Copy: new object copied

You must specify a type for every array element. Char arrays are useful for character-level manipulations. An Object array is useful for storing derived types with base references.

Byte ArrayChar ArrayObject ArrayChaos

Arrays are not limited to one dimension. You can use two-dimensional arrays—arrays that are square or rectangular. You can even use jagged arrays. We first demonstrate 2D arrays.

2D Array

Next:Jagged arrays are basically arrays of arrays. In them each array element is actually a whole separate array.

Jagged Array

Further:Multidimensional arrays beyond two dimensions are possible. These use a similar syntax form.

3D 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

Summary

The VB.NET programming language

Arrays are an important type. They are used inside other types such as List and Dictionary to implement those types' storage. They have a lot of performance advantages. This makes them a critical part of your programming skill set.

VB.NET