Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
ResetList()
End If
End Sub 'Page_Load
Sub ResetList()
HashTableSample.table.Clear()
HashTableSample.table.Add(5123, "Jay")
HashTableSample.table.Add(2569, "Brad")
HashTableSample.table.Add(1254, "Brian")
HashTableSample.table.Add(6839, "Seth")
HashTableSample.table.Add(3948, "Rajesh")
HashTableSample.table.Add(1930, "Lakshan")
HashTableSample.table.Add(9341, "Kristian")
HashTableSample.table.Add(7921, "Stephen")
HashTableSample.table.Add(2839, "Tom")
HashTableSample.table.Add(1829, "Kit")
MakeList()
End Sub
Public Sub MakeList()
lstNames.Items.Clear()
Dim o As [Object]
For Each o In HashTableSample.table.Keys
lstNames.Items.Add((o.ToString() + ": " + HashTableSample.table(o)))
Next o
End Sub
Class HashTableSample
Public Shared table As New Hashtable()
Public Shared Function FindEntry(inKey As [Object], value As [Object]) As String
Dim strWriter As New StringWriter()
Console.SetOut(strWriter)
Console.WriteLine()
If inKey Is Nothing OrElse inKey = "" Then
inKey = 0
End If
Dim key As Int32
Try
key = Int32.Parse(inKey.ToString)
Catch generatedExceptionVariable0 As Exception
key = 0
End Try
If Not (value Is Nothing) Then
If table.ContainsValue(value) Then
Console.WriteLine("Yes, Value '{0}' was found in the list!", value)
Else
Console.WriteLine("Sorry, Employee '{0}' could not be found.", value)
End If
Else
If table.Contains(key) Then
Console.WriteLine("Yes, ID '{0}' was found in the list!", key)
Else
Console.WriteLine("Sorry, Employee ID '{0}' could not be found.", key)
End If
End If
Console.WriteLine()
Console.WriteLine("Now we will print out the entire list of employees.")
Console.WriteLine("ID" + ControlChars.Tab + "Name")
Console.WriteLine("----" + ControlChars.Tab + "-----------")
Dim d As DictionaryEntry
For Each d In table
Console.WriteLine("{0}" + ControlChars.Tab + "{1}", d.Key, d.Value)
Next d
Return strWriter.ToString()
End Function
Public Shared Function AddEntry(key As String, value As String) As Boolean
Try
Dim i As Int32 = Convert.ToInt32(key)
table.Add(i, value)
Return True
Catch generatedExceptionVariable0 As Exception
Return False
End Try
End Function
End Class
Public Sub btnID_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(empID.Text, Nothing) + "</PRE>"
btnClear.Visible = True
End Sub
Public Sub btnName_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(0, empName.Text) + "</PRE>"
btnClear.Visible = True
End Sub
Public Sub btnClear_Click([source] As [Object], e As EventArgs)
output.Text = ""
empName.Text = ""
empID.Text = ""
btnClear.Visible = False
End Sub
Public Sub btnReset_Click([source] As [Object], e As EventArgs)
ResetList()
End Sub
Public Sub btnAdd_Click([source] As [Object], e As EventArgs)
If txtAddKey.Text = "" OrElse txtAddVal.Text = "" Then
output.Text = "ADD FAILED: Please ensure you enter both a key, and value entry"
btnClear.Visible = True
Else
If HashTableSample.AddEntry(txtAddKey.Text, txtAddVal.Text) Then
MakeList()
btnClear_Click(Nothing, Nothing)
Else
output.Text = "ADD FAILED: Please ensure the following:<br>" + "1) The key is a valid integer<br>" + "2) The key does not already exist in the list"
btnClear.Visible = True
End If
End If
End Sub
End Class