A brief overview of Arrays and utility functions in Visual Basic

Arrays in Visual Basic are handled differently then their counter parts in Java, PHP, and Ruby. The first difference is that you specify array index by use of parenthesis not square brackets.

Java Array Initialization

String[] myArray = new String[20];

Visual Basic Array Initialization

Dim myArray(19) As String

Looking over the two arrays you may think that the Array in VB only can hold 19 elements this is incorrect. Visual Basic uses a 0 based index meaning that in actuality it holds elements at indexes 0 through 19 or 20 total items.

Fixed Length Array

When you specify the array’s dimension when declaring the array you have created it as a Fixed length Array this means that you can not change its size and decide you want to add a 21st item to an array of 20 items. To access or set an item in an array you simply need to call the variable with it’s index wrapped in parenthesis.

Dim myArray(2) As String

myArray(0) = "Matthew"
myArray(1) = "Apollo"
myArray(2) = "Joseph"

Dim aName As String
aName = myArray(1)

Dynamic Length Array

Sometimes you will not know the full size of the array you will need to create. In these cases you can create a Dynamic Length Array, these arrays allow you to change their size by re defining their length while preserving their contents. To define your Dynamic Array you simply don’t specify the length of the array in its initial creation.

Dim myArray() As String

Now before you can use the array to store elements you will need to redefine it with a length to do this you will need to use the ReDim command.

ReDim myArray(2) As String

If we need to increase the size again we would simply use ReDim again, however this will cause any existing data to be lost. If you want to keep all current elements in the dynamic you will need to use the Preserve keyword.

ReDim Preserve myArray(5) As String

Array Utility/Helper Functions

Split

If you have a delimited String that you want to break up based on a character the Split function is what you will want to use. It takes a String and a delimiter and returns an Array containing sub strings between the delimiter.

Dim userArray() As String
Dim userNames As String

nameArray= "Matthew,Doug,Sarah,Jill"
userArray = Split( nameArray, "," )

' The generated array would contain 4 elements
userArray[0] 'Matthew
userArray[1] 'Doug
userArray[2] 'Sarah
userArray[3] 'Jill

Join

Conversely you may find that you want to take an array and concatenate the elements with some value between the elements. In this case you would use the Join method. If you don’t want any delimiter between the values you simply don’t input a 2nd parameter and empty string will be used by default.

Dim userArray(3) As String
Dim userNames As String

userArray[0] = Matthew
userArray[1] = Doug
userArray[2] = Sarah
userArray[3] = Jill

userNames = Join(userArray, "," )
usernames 'Matthew,Doug,Sarah,Jill

As shown Visual Basics has all the common functionality of arrays in other programming languages the key to remember is that you use parenthesis not square brackets when accessing element indexes.

// Visual Basic //

Comments & Questions

Add Your Comment