ASP 101 - Active Server Pages 101 array.aspx
<%@ Page Language="VB" %>
<script runat="server">
	Sub Page_Load(sender As Object, e As EventArgs)
		Dim arrNames(3) As String  ' Declare a static array with 4 elements
		Dim arrAges() As Integer   ' Declare a dynamic array that we can resize
		' Let's start with our static array.  Static doesn't mean you
		' can't change the values of the elements.  It simply means you
		' can't change the number of elements in the array.  The values
		' of the elements themselves can change as often as they need to.
		arrNames(0) = "Susan"
		arrNames(1) = "Andy"
		arrNames(2) = "Fred"
		arrNames(3) = "Kelly"
		' Notice that even though I used the number 3 to dimension the
		' array there are indeed four elements which I can insert values
		' into.  This is because arrays in VB.NET are zero based meaning
		' the first item has an index of zero.
		' Display what our array currently contains. I wrapped this into
		' a function since I'm doing it a lot.  See below for implementation.
		litStatic.Text = TableFromArray(arrNames)
		' As I mentioned earlier... even with a static array I can
		' reassign values if I need to.  I'm going to set the value of
		' the second element to the value of the first element
		' overwriting the existing value... effectively changing "Andy"
		' to "Susan."  For good measure I'm also going to change the
		' first element to "John" since I'm feeling left out.
		arrNames(1) = arrNames(0)
		arrNames(0) = "John"
		' Here's what the array now contains:
		litStatic2.Text = TableFromArray(arrNames)
		' Now on to our dynamic array.  Before I can use a dynamic array
		' I need to tell it how many elements I want to be able to put
		' into it.  The ReDim command allows us to redimension an array
		' to whatever size we need.  I'm setting it to 2 elements so I
		' use an upper bound of 1.
		ReDim arrAges(1)
		arrAges(0) = 15
		arrAges(1) = 20
		' Show what our array currently contains:
		litDynamic.Text = TableFromArray(arrAges)
		' Now I'm going to ReDim the array again just to illustrate. I
		' want to keep the existing data so I include the Preserve
		' command.  If I didn't all the data already in the array would
		' be lost.  You can also ReDim to change the number of
		' dimensions in a dynamic array, but that's a little beyond the
		' scope of this basic intro so here goes the simple resize up
		' to 4 elements...
		ReDim Preserve arrAges(3)
		' Add more values:
		arrAges(2) = 25
		arrAges(3) = 30
		' Once again show what our array currently contains:
		litDynamic2.Text = TableFromArray(arrAges)
		' The third column from the classic ASP version used to illustrate
		' using the VBScript Array function to define and populate the
		' array.  Since it no longer exists, we obviously can no longer
		' illustrate it.  Just letting you know why there are now only
		' two columns.
	End Sub
	' Takes a 1 dimensional array and simply spits out its values
	' in a table format with a note saying how big the array is.
	Function TableFromArray(ArrayToShow As Array) As String
		Dim I           As Integer  ' Simple Looping Var
		Dim iArraySize  As Integer  ' Var to store array size
		Dim myStringBuilder As StringBuilder
		' If you want to know how big an array is, you can use this
		' to find out. This even works in VB6 where they don't have
		' to be zero-based.  The LBound and UBound return the
		' indecies of the lowest and highest array elements so to
		' get the size we take the difference and add one since you
		' can store a value at both end points.
		iArraySize = (UBound(ArrayToShow) - LBound(ArrayToShow)) + 1
		' In this .NET version I'm using a StringBuilder object to
		' avoid the all-consuming evil of string concatenation.
		myStringBuilder = New StringBuilder
		myStringBuilder.Append("<p>The array has " & iArraySize _
			& " elements.  They are:</p>" & vbCrLf)
		myStringBuilder.Append("<table border=""1"">" & vbCrLf)
		myStringBuilder.Append("<thead>" & vbCrLf)
		myStringBuilder.Append("<tr>")
		myStringBuilder.Append("<th>Index</th>")
		myStringBuilder.Append("<th>Value</th>")
		myStringBuilder.Append("</tr>" & vbCrLf)
		myStringBuilder.Append("</thead>" & vbCrLf)
		myStringBuilder.Append("<tbody>" & vbCrLf)
		' Simple loop over a table outputting a row for each element
		For I = LBound(ArrayToShow) To UBound(ArrayToShow)
			myStringBuilder.Append("<tr>")
			' Write out the index of the element we're currently on
			myStringBuilder.Append("<td>" & I & "</td>")
			' Write out the value of the element we're currently on
			myStringBuilder.Append("<td>" & ArrayToShow(I) & "</td>")
			myStringBuilder.Append("</tr>" & vbCrLf)
		Next 'I
		myStringBuilder.Append("</tbody>" & vbCrLf)
		myStringBuilder.Append("</table>" & vbCrLf)
		TableFromArray = myStringBuilder.ToString
		myStringBuilder = Nothing
	End Function
</script>
<html>
<head>
<title>ASP.NET Proper Case Sample</title>
</head>
<body>
<!--
This outer table is just so you don't end up having to scroll.
It really has nothing to do with the sample.
-->
<table border="1">
	<tr>
		<td valign="top">
			<h3>Static Array</h3>
			<asp:Literal id="litStatic" runat="server" />
			<asp:Literal id="litStatic2" runat="server" />
		</td>
		<td valign="top">
			<h3>Dynamic Array</h3>
			<asp:Literal id="litDynamic" runat="server" />
			<asp:Literal id="litDynamic2" runat="server" />
		</td>
	</tr>
</table>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/array_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>