I am having trouble with the code on 154, I am getting two errors:
For [u]DialogResult.Yes </u> I get the folloing error: Access of shared member, constant member,enum member or nested type through an instance; qualifying expression will not be evaluated.
For [u]End Get</u> I get the following error: Property "Selected Customer" Dosen't return a value on all code paths. A null reference exception could occure at run time whn the result is used.
My code follows:
Public Class Form1
'Form level members
Private objCustomers As New ArrayList
Public Sub CreateCustomer(ByVal firstName As String, ByVal lastName As String, ByVal email As String)
'Declare a customer object
Dim objNewCustomer As Customer
'Create the new customer
objNewCustomer.FirstName = firstName
objNewCustomer.LastName = lastName
objNewCustomer.Email = email
'Add the new customer to the list
objCustomers.Add(objNewCustomer)
'Add the new customer to the ListBox control
lstCustomers.Items.Add(objNewCustomer)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
'Create some customer
CreateCustomer("Darrel", "Hilton", "
[email protected]")
CreateCustomer("Frank", "Peoples", "
[email protected]")
CreateCustomer("Bill", "Scott", "
[email protected]")
End Sub
Public Sub DisplayCustomer(ByVal customer As Customer)
'Display the customer details on the form
txtFirstName.Text = customer.FirstName
txtLastName.Text = customer.LastName
txtEmail.Text = customer.Email
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'If no customer is selected in the ListBox then...
If lstCustomers.SelectedIndex = -1 Then
'Display a message
MessageBox.Show("You must select a customer to delete,", "Structure Demo")
'Exit this method
Exit Sub
End If
'Prompt the user to delete the selected customer
If MessageBox.Show("Are your sure you want to delete " & _
SelectedCustomer.Name & "?", "Structure Demo", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _
DialogResult.Yes Then
'Get the customer to be deleted
Dim objCustomerToDelete As Customer = SelectedCustomer
'Remove the customer from the ArrayList
objCustomers.Remove(objCustomerToDelete)
'Remove the customer from the ListBox
lstCustomers.Items.Remove(objCustomerToDelete)
End If
End Sub
Public ReadOnly Property SelectedCustomer() As Customer
Get
If lstCustomers.SelectedIndex <> -1 Then
'Return the selected customer
Return lstCustomers.Items(lstCustomers.SelectedIndex)
End If
:D
End Property
End Class