ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

ASP Source Code:
Dim arrNames(3)     ' Declare a static array with 4 elements
Dim arrAges()       ' Declare a dynamic array that we can resize
Dim arrColors       ' Declare a standard variable that we can
                    ' later assign an array to if we need to.
This outer table is just so you don't end up having to scroll
so far down to get to the view source buttons.  It really has
nothing to do with the sample.
<table border="1">
<td valign="top">
<h3>Static Array</h3>
' 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 all arrays in VBScript are zero based
' meaning the first item has an index of zero.  Geeks think in
' binary and like to be different... what can I say?
' Display what our array currently contains. I wrapped this into
' a subroutine since I'm doing it a lot.  See the sub down at
' the bottom for implementation details.
' 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:
<td valign="top">
<h3>Dynamic Array</h3>
' 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:
' 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 another value:
arrAges(2) = 25
arrAges(3) = 30
' Once again show what our array currently contains:
<td valign="top">
<h3>Array Using the Array Function</h3>
' Now we come to an interesting point.  Since all variables
' in VBScript are really variants, you can assign an array to
' a variable that wasn't originally defined as one!
arrColors = Array("red", "green", "blue")
' Show the values in our new array:
' Takes a 1 dimensional array and simply spits out its values
' in a table format with a note saying how big the array is.
Sub ShowArrayInTable(ArrayToShow)
	Dim I           ' Simple Looping Var
	Dim iArraySize  ' Var to store array size
	' If you want to know how big an array is, you can use this
	' to find out. This even works in VB 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
	Response.Write "<p>The array has " & iArraySize _
		& " elements.  They are:</p>" & vbCrLf
	Response.Write "<table border=""1"">" & vbCrLf
	Response.Write "<thead>" & vbCrLf
	Response.Write "<tr>" & vbCrLf
	Response.Write "<th>Index</th>" & vbCrLf
	Response.Write "<th>Value</th>" & vbCrLf
	Response.Write "</tr>" & vbCrLf
	Response.Write "</thead>" & vbCrLf
	Response.Write "<tbody>" & vbCrLf
	' Simple loop over a table outputting a row for each element
	For I = LBound(ArrayToShow) To UBound(ArrayToShow)
		Response.Write "<tr>" & vbCrLf
		' Write out the index of the element we're currently on
		Response.Write "<td>" & I & "</td>" & vbCrLf
		' Write out the value of the element we're currently on
		Response.Write "<td>" & ArrayToShow(I) & "</td>" & vbCrLf
		Response.Write "</tr>" & vbCrLf
	Next 'I
	Response.Write "</tbody>" & vbCrLf
	Response.Write "</table>" & vbCrLf
End Sub

Back the the Sample Output

Back to the Sample Index

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers