1
« en: Jueves 29 de Octubre de 2009, 12:51 »
Hola:
En el siguiente código recibo un warnig en la linea 59:
Warning 1: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
Y en la línea 78 recibo este Warning:
Warning 2 Property 'SelectedCustomer' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
El programa compila y funciona bien, pero no entiendo la razón de los Warnings. Alguna idea ?
1: Public Class Form1
2:
3: 'Form level members
4: Private objCustomers As New ArrayList
5:
6:
7: Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnTest.Click
8: 'Create some customers
9: CreateCustomer("Darrel", "Hilton", "dhilton@somecompany.com")
10: CreateCustomer("Frank", "Peoples", "fpeoples@somecompany.com")
11: CreateCustomer("Bill", "Scott", "bscott@somecompany.com")
12:
13: End Sub
14:
15: Public Sub DisplayCustomer(ByVal customer As Customer)
16: 'Display the customer details on the form
17: txtName.Text = customer.Name
18: txtFirstName.Text = customer.FirstName
19: txtLastName.Text = customer.LastName
20: txtEmail.Text = customer.Email
21: End Sub
22:
23: Public Sub CreateCustomer(ByVal firstName As String, _
24: ByVal lastName As String, ByVal email As String)
25:
26: 'Declare a Customer object
27: Dim objNewCustomer As Customer
28:
29: 'Create the new customer
30: objNewCustomer.FirstName = firstName
31: objNewCustomer.LastName = lastName
32: objNewCustomer.Email = email
33:
34: 'Add the new customer to the list
35: objCustomers.Add(objNewCustomer)
36:
37: 'Add the new customer to the ListBox control
38: lstCustomers.Items.Add(objNewCustomer)
39: End Sub
40:
41: Private Sub btnDelete_Click(ByVal sender As System.Object, _
42: ByVal e As System.EventArgs) Handles btnDelete.Click
43:
44: 'If no customer is selected in the ListBox then...
45: If lstCustomers.SelectedIndex = -1 Then
46:
47: 'Display a message
48: MessageBox.Show("You must select a customer to delete.",
_
49: "Structure Demo")
50:
51: 'Exit this method
52: Exit Sub
53: End If
54:
55: 'Prompt the user to delete the selected customer
56: If MessageBox.Show("Are you sure you want to delete " & _
57: SelectedCustomer.Name & " ?", "Structure Demo", _
58: MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _
59: DialogResult.Yes Then
60:
61: 'Get the customer to be deleted
62: Dim objCustomerToDelete As Customer = SelectedCustomer
63:
64: 'Remove the customer from the ArrayList
65: objCustomers.Remove(objCustomerToDelete)
66:
67: 'Remove the customer from the ListBox
68: lstCustomers.Items.Remove(objCustomerToDelete)
69: End If
70: End Sub
71:
72: Public ReadOnly Property SelectedCustomer() As Customer
73: Get
74: If lstCustomers.SelectedIndex <> -1 Then
75: 'Return the selected customer
76: Return CType(lstCustomers.Items(lstCustomers.SelectedIndex),
Customer)
77: End If
78: End Get
79: End Property
80:
81: End Class
82:
Gracias por vuestra ayuda.
Xavier.