warnings in the following code

xmonfort

New member
Joined
Oct 29, 2009
Messages
2
Programming Experience
Beginner
In the following code i get a warning at line 59:
Warning 1: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

and.. At line 78 I get this 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.

The program compiles and runs well, but i cant' undesrtand the reason for these warnings. Any 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:

Thanks

Xavier.
 
You have an If statement in your getter, and you're not returning anything in case of Else. Return Nothing in this case.
 
The second warning tells it all. in this property you are not returning a value on all code paths. if lstCustomers.SelectedIndex = -1 then that property never returns anything. Any property/function MUST return something.

VB.NET:
   Public ReadOnly Property SelectedCustomer() As Customer
 Get
            If lstCustomers.SelectedIndex <> -1 Then
                'Return the selected customer
 Return CType(lstCustomers.Items(lstCustomers.SelectedInde x),Customer)
            End If
        End Get
    End Property
 
The second warning tells it all. in this property you are not returning a value on all code paths. if lstCustomers.SelectedIndex = -1 then that property never returns anything. Any property/function MUST return something.

VB.NET:
   Public ReadOnly Property SelectedCustomer() As Customer
 Get
            If lstCustomers.SelectedIndex <> -1 Then
                'Return the selected customer
 Return CType(lstCustomers.Items(lstCustomers.SelectedInde x),Customer)
            End If
        End Get
    End Property

Thanks a lot.

I have solved this second warning by adding this sentence:

Else: return Nothing

before de End if at line 77
 
In case of nested Ifs it can get complex adding the Else everywhere, what you can do is just add as last line of method the 'Return Nothing', in all other cases that matches some clause the method would return specific results. Even with your simple If-Else I would just skip the Else. F.ex:
VB.NET:
If SelectedIndex<>1 Then
  Return aCustomer
End If
Return Nothing
 
Back
Top