Arrays in ASP.NET / VB.NET

Arrays are variables that can store more than one value of the same data type.

In example 1 below we declare and create an array ‘myArray’ that will hold string values. Our array specifies 3 as the highest element (or upper bound as it's called) of our array.

The first element or lower bound in our array has a starting index of 0, so our example array has 4 elements. The first element holds the value ‘BMW’ and so on.

Example 1

Dim myArray(3) As String
myArray(0) = "BMW"
myArray(1) = "Mercedes"
myArray(2) = "Honda"
myArray(3) = "Porshe"

Example 2 creates an array of integers. Like the example above the array has 4 elements, with a starting index of 0 and ending index of 3.

Example 2

Dim myArray(4) As Integer
myArray(0) = "2"
myArray(1) = "55"
myArray(2) = "33"
myArray(3) = "55656"

In our first examples we specified the number of array elements by declaring the highest/last element or upper bound of the array.

How to Create an Array without Specifying the Number of Elements

We could also create an array without specifying a number of elements, and then set the initial values for each item of the array.

Our example below declares a string array, and the 4 elements and their values are provided enclosed in the {} braces, with a comma as a separator.

Dim myArray() As String
myArray = New String(){“BMW”,”Mercedes”,”Honda”,”Porshe”}

Or we could shorten it like this:

Dim myArray() As String = {“BMW”,”Mercedes”,”Honda”,”Porshe”}

In Part 1 learn the basics of Arrays in ASP.NET
In Part 2 learn how to Reference an Array Element
In Part 3 learn how to Loop through an array in ASP.NET and VB.NET

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.