Reference an Array Element in ASP.NET / VB.NET

In part 1 we discussed the basics of Arrays in ASP.NET. In our code below on the page load event we declare and create our string array 'myArray' with a higher bound of 3.

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

We then assign values to each element. Next we bind the value of the 4th element to the text property of the literal control 'ltlArray'. When the page is run the browser will display the value ‘Porshe’.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

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

        ltlArray.Text = myArray(3) 'bind the value of the 4th element to our literal control

    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
<title>Arrays in ASP.NET</title>
</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Literal ID="ltlArray" runat="server" />

    </div>

    </form>

</body>

</html>

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

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

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

Get your best asp web hosting provider now and save 25%