How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask LogicPro Your Own Question
LogicPro
LogicPro, Computer Software Engineer
Category: Programming
Satisfied Customers: 12986
Experience:  Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
29751445
Type Your Programming Question Here...
LogicPro is online now
A new question is answered every 9 seconds

Imports System.Collections.generic Public Class CollectionsForm

This answer was rated:

Imports System.Collections.generic

Public Class CollectionsForm
Private CustomersDictionary As Dictionary(Of String, Customer)

Private Sub AddButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles AddButton.Click
Dim KeyString As String

If NameTextBox.Text "" Then
Try
Dim ACustomer As New Customer(NameTextBox.Text, Decimal.Parse(NumberTextBox.Text))
KeyString = ACustomer.Number.GetHashCode.ToString
CustomersDictionary.Add(KeyString, ACustomer)
DisplayList()
With NameTextBox
.Clear()
.Focus()
End With
NumberTextBox.Clear()
PHTextBox.Clear()
Catch err As FormatException
With NumberTextBox
.SelectAll()
.Focus()
End With
MessageBox.Show("Numeric entry required." & _
ControlChars.NewLine & NameTextBox.Text & _
" Not Added.", "A Collection of Customers")
Catch err As Exception
With NameTextBox
.SelectAll()
.Focus()
End With
MessageBox.Show("Duplicate Customer name." & _
ControlChars.NewLine & NameTextBox.Text & _
" Not Added." & err.Message, "A Collection of Customers")
End Try
Else
NameTextBox.Focus()
MessageBox.Show("Please enter a name.", _
"A Collection of Customers")
End If
End Sub

Private Sub RemoveButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RemoveButton.Click

With CustomersListBox
If .SelectedIndex -1 Then
Dim KeyString As String = .SelectedItem.GetHashCode.ToString
CustomersDictionary.Remove(KeyString)
DisplayList()
Else
MessageBox.Show("Select a Customer from the list.", _
"A Collection of Customers")
End If
End With
End Sub

Private Sub DisplayButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles displayButton.Click
Dim ACustomer As Customer


With CustomersListBox
If .SelectedIndex -1 Then
Dim KeyString As String = .SelectedItem.GetHashCode.ToString
****ACustomer = CType(CustomersDictionary.Item(KeyString), Customer)***
MessageBox.Show("Customer Name: " & ACustomer.Name & vbCrLf & "Customer Number: " & ACustomer.Number & vbCrLf & "Phone Number: " & ACustomer.PH)
Else
MessageBox.Show("Select a Customer from the list.", _
"A Collection of Customers")
End If
End With
End Sub

Private Sub DisplayList()
Dim ACustomer As Customer
With CustomersListBox
.Items.Clear()
For Each Pair As KeyValuePair(Of String, Customer) In CustomersDictionary

ACustomer = CType(Pair.Value, Customer)
.Items.Add(ACustomer.Name)
Next

End With
End Sub

Private Sub CollectionsForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

CustomersDictionary = New Dictionary(Of String, Customer)
End Sub
End Class

Getting a KeyNotFoundException was unhandled on line 73. I surrounded it with asterisks for ease of finding).
Hi,
Please provide the zipped project folder.
Customer: replied 5 years ago.
http://wikisend.com/download/156368/Claytor's Cottages.zip
Please wait while I check it
I checked it. It is not even adding the second customer. The keystring is always zero so it is saying duplicate customer.
We can take customer name as keystring. it will ensure that same customer is not added again. Then when you retrieve the recod and compare customer name.
Hope it clears. Ask me if not.

LogicPro and other Programming Specialists are ready to help you