Link to home
Start Free TrialLog in
Avatar of cbittner
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(lstCustomers.SelectedIndex)
        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(lstCustomers.SelectedIndex)
        End If
    End Function
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial