cbittner
asked on
Returning a value as a property versus from a function, what is difference
I am learnng VB. NET, and am working through some sample code in Beginning VB.NET 2003. In this example, a customer structure is returned as a property (see A), and that works fine, but I started to wonder why does the structure value need returned as a property, so I tried returning the value from a function (see B) and it works fine too, so I am wondering when would you return a value as a property versus returning a value from a function?
Calling code:
Dim DeleteCustomer As Customer = SelectedCustomer ' this line calls for the value to be returned
A - Book sample Code
Public ReadOnly Property SelectedCustomer() As customer
Get
'do we have a selection?
If lstCustomers.SelectedIndex <> -1 Then
'return the selected cusotmer...
Return lstCustomers.Items(lstCust omers.Sele ctedIndex)
End If
End Get
End Property
B - Using a function return
Public Function SelectedCustomer() As customer
'do we have a selection?
If lstCustomers.SelectedIndex <> -1 Then
'return the selected cusotmer...
Return lstCustomers.Items(lstCust omers.Sele ctedIndex)
End If
End Function
Calling code:
Dim DeleteCustomer As Customer = SelectedCustomer ' this line calls for the value to be returned
A - Book sample Code
Public ReadOnly Property SelectedCustomer() As customer
Get
'do we have a selection?
If lstCustomers.SelectedIndex
'return the selected cusotmer...
Return lstCustomers.Items(lstCust
End If
End Get
End Property
B - Using a function return
Public Function SelectedCustomer() As customer
'do we have a selection?
If lstCustomers.SelectedIndex
'return the selected cusotmer...
Return lstCustomers.Items(lstCust
End If
End Function
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.