<%@ Import Namespace="System.Drawing" %>
<script runat="server" language="VB">
Dim myArray(4) as Student
Sub Page_Load(sender as Object, e as EventArgs)
'Create an array of students
myArray(0) = New Student("Scott", 24)
myArray(1) = New Student("Jisun", 23)
myArray(2) = New Student("Spacey", 21)
myArray(3) = New Student("John", 23)
myArray(4) = New Student("Josh", 20)
Dim i as Integer
ltlArrayPresort.Text = "<p><ul>"
For i = 0 to myArray.Length - 1
ltlArrayPresort.Text &= "myArray[" & i & "] : <b>Student Info...</b> Name = " & myArray(i).Name & _
", Age = " & myArray(i).Age & "<br />"
Next i
ltlArrayPresort.Text &= "</ul></p>"
'Sort the array
Array.Sort(myArray, New CompareCustomDataType)
ltlArraySort.Text = "<p><ul>"
For i = 0 to myArray.Length - 1
ltlArraySort.Text &= "myArray[" & i & "] : <b>Student Info...</b> Name = " & myArray(i).Name & _
", Age = " & myArray(i).Age & "<br />"
Next i
ltlArraySort.Text &= "</ul></p>"
End Sub
Sub searchArray(sender as Object, e as EventArgs)
Dim location as Integer
Dim studentTemp as New Student()
studentTemp.Name = txtSearchTerm.Text
location = Array.BinarySearch(myArray, studentTemp, New CompareCustomDataType)
If location < 0 then
lblResults.Text = "The student " & txtSearchTerm.Text & _
" was not found in the array."
lblResults.ForeColor = Color.Red
Else
lblResults.Text = "The student " & txtSearchTerm.Text & _
" was found at position " & location & "."
lblResults.ForeColor = Color.Black
End If
End Sub
</script>
<form runat="server">
The array of elements is given as: <asp:literal id="ltlArrayPresort" runat="server" /><p>
The array of elements sorted is given as:
<asp:literal id="ltlArraySort" runat="server" /><p>
<b>Search for a Student by Name</b><br />
<asp:textbox id="txtSearchTerm" runat="server" Columns="8" /><br />
<asp:Button runat="server" Text="Search Array Using BinarySearch"
OnClick="searchArray" />
<p>
<asp:label runat="server" id="lblResults" />
</form>
|