An array is a list of values. All values must be of the same
type. The values are arranged in a consecutive manner each after another. This
organizatopm makes it easy to retrieve and optionally manipulate the items.

Introduction to Creating an Array

The most fundamental formula to create an array is:

Dim variable-name(count) As data-type

Like a normal variable, an array must have a name, which is
followed by parentheses. In the parentheses, specify the number of elements or
items in the array. When creating an array, you must decide what type of
values each element of the array would be. A simple array can be made of
primitive types of values. Here is an example:

<%
Dim classifications(4) As String
%>

Introduction to Adding Values to an Array

There are various ways you can specify the items and values
of an array. One technique is to use the following formula:

Dim variable-name = New data-type() { values }

Notice that the parentheses after the data type are empty,
followed by curly brackets. In the curly brackets, specify the value of each
item and separate them with commas. Here is an example:

Once the array has been created, you can access each one of
its elements. To do this, type the name of the array followed by parentheses. In
the parentheses, enter the position, called index, of the desired item. The
first member of the array is at position, or has the index, 0. The second item
has the index 1. The third item has the index 2, and so on. Here is an example:

By accessing an item by its position, you can specify its
value. In fact, if you declare an array variable as we did in our introduction (Dim SocialSciences(15) As String),
you can access each element using its index and assign the desired value. Here
are examples:

To support arrays, the .NET Framework provides a class
named Array. The Array class is defined in the
System namespace.
Whenever you create or use an array, it is actually an object of type Array.

The Length of an Array

As seen in our introduction, if you declare an array
variable without initializing it, you must specify the number of
elements of the array. Here is the example we saw:

<%
Dim classifications(4) As String
%>

If the array exists already, to assist you with finding
out the number of elements in it, the Array class provides
a read-only property named Length:

Public ReadOnly Property Length As Integer

Therefore, to know the number
of items that an array contains, get the value of the Length
property. If you are using a For...To...Next loop to access each
element of an array, the value for the To clause will be the Length - 1.

Arrays and Procedures/Functions

Returning an Array From a Function

You can create a function that returns an array. To specify
the returned type of the function, type As followed by the data type and
empty parentheses. In the body
of the function, you can use a situation that produces an array and returns i.
Here is an example:

Like a regular variable, an array can be passed to a
function. In the parentheses of the procedure or function, make sure the name of
the parameter has parentheses. In the body of the procedure, use the parameter
as you would an array. Here is an example:

The Visual Basic language provides a mechanism to pass a
different number of arguments every time a procedure or function is called. This
is done by passing the parameter with a keyword named ParamArray that is
followed by the name of the parameter and its parentheses. In the body of the
procedure, treat the parameter as a normal array. Here is an example:

<script runat="server">
Sub Display(ByVal ParamArray summary() As Double)
For Each day As Double In summary
Response.Write(FormatNumber(day) & "<br>")
Next day
End Sub
</script>

When calling the procedure, pass the desired number of
arguments. Here is an example:

If the function is taking more than one parameter and one of
them must be a ParamArray type, this type must be the last in the
parameter list.

Arrays and Classes

An Array of Objects

An array can be made of elements that are each a
composite type. That is, each element can be of an object of a class type. Of
course, you must have a class first. You can use one of the many built-in
classes of the .NET Framework or you can create your own class. You can create an array
of objects without specifying its values, using the same formula of our
introduction. Here is an example:

You can create a class member that is an array. It is
created and used like an array variable.

A Property as an Array

A property can be an array type. When specifying the data type
of the
property, make sure it is followed by parentheses. Here is an example:

<script runat="server">
Public Class MetroStation
Public Sub New(ByVal number As Integer,
ByVal name As String,
ByVal longTermParting As Boolean,
ByVal shortTermParting As Boolean)
Me.StationNumber = number
Me.StationName = name
Me.DailyParking = longTermParting
Me.MeteredParking = shortTermParting
End Sub
Public Property StationNumber As Integer
Public Property StationName As String
Public Property Line As String
Public Property DailyParking As Boolean
Public Property MeteredParking As Boolean
End Class
Public Class MetroLine
Public Property Line As String
Public Property Stations As MetroStation()
End Class
</script>

If the property is not self implemented, make it returns an
array or one is assigned to it. You can do this in
a constructor or in an event that would be fired before any other event that
would use the array. This type of initialization is usually done in a Load
event of a form. After initializing the array, you can then access in another
method or another event of the form. Here is an example:

<script runat="server">
Public Class MetroStation
Public Sub New(ByVal number As Integer,
ByVal name As String,
ByVal longTermParting As Boolean,
ByVal shortTermParting As Boolean)
Me.StationNumber = number
Me.StationName = name
Me.DailyParking = longTermParting
Me.MeteredParking = shortTermParting
End Sub
Public Property StationNumber As Integer
Public Property StationName As String
Public Property Line As String
Public Property DailyParking As Boolean
Public Property MeteredParking As Boolean
End Class
Public Class MetroLine
Private places As MetroStation()
Public Property Line As String
Public Property Stations As MetroStation()
Get
Return places
End Get
Set(value As MetroStation())
places = value
End Set
End Property
End Class
</script>

You can then use the property like a regular array. Here is
an example:

Because an array is primarily a series of objects or
values, there are various pieces of information you would get interested
from it. Typical operations include:

Adding elements to the array

Re-arranging the list or the order of elements in the array.

Finding out whether the array contains a certain element

If the array contains a certain element, what index that element has

Although you can write your own routines to perform these
operations, the Array class provides the methods you would need to get
the necessary information.

Adding an Element to an Array

We have seen that, using the () operator, you can add a new
element to an array. Still, to support this operation, the Array class is
equipped with the SetValue() method that is overloaded with various
versions. Here is an example that adds an element to the third position of the
array:

When the Array.SetValue() method is called, it
replaces the element at the indicated position.

Getting an Element From an Array

The reverse of adding an item to an array is to retrieve
one. To support this operation, the Array
class is equipped with the GetValue() method that comes in various
versions. Here is an example of calling it:

When calling this method, make sure you provide a valid
index, if you do not, you would get an IndexOutOfRangeException
exception.

Checking the Existence of an Element

Once some elements have been added to an array, you can try
to locate one. One of the most fundamental means of looking for an item consists
of finding out whether a certain element exists in the array. To support this
operation, the Array class is equipped with the Exists() method whose
syntax is:

This is a generic method that takes two arguments. The first
is the array on which you will look for the item. The second argument is a
delegate that specifies the criterion to apply. Here is an example:

One of the most valuable operations you can perform on an
array consists of looking for a particular element. To assist you with this, the
Array class is equipped with the Find() method. Its syntax is:

This generic method takes two arguments. The first argument
is the name of the array that holds the elements. The second argument is a
delegate that has the criterion to be used to find the element. Here is an
example: