Solved

Returning a value as a property versus from a function, what is difference

Posted on 2004-10-05
7
210 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:cbittner
7 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 12232979
If the property is ReadOnly as in this case, then their really is no difference.  If you need to set the variable then a function will not work unless you pass it in as a parameter and this will not have the same syntax and behaviour is if you had used a property.

Other than that, it's really a matter of preference and coding style.  A property implies that you are getting/setting a value with possibly some kind of range/validity checking.  A function implies that some kind of calculcation or change is being made.

~IM
0
 
LVL 7

Accepted Solution

by:
J_Mak earned 100 total points
ID: 12233023
Well if you look at the properties window in the VB .NET IDE, you'll notice you can set them as True, False, or a numeric or string value, depending on what kind of property it is. I believe properties can be set a value, and you can get a value from it, again depending on whether it is read only or not. They aren't naturally used as functions which perform calculations, that is the job of a Function. The Properties window, as you may have noticed does not change any value that you set. It is your job as a programmer to write functions which manipulate properties and do whatever you want with their values. I hope that helps. Cheers.
0
 
LVL 7

Assisted Solution

by:J_Mak
J_Mak earned 100 total points
ID: 12233055
You can implicitly use properties to act like functions, but I believe that it is used to set or get a member variable of a class or form. For example, a class may define a Private variable which obviously cannot be accessed from outisde this class. If that class though defines a property which gets or sets the variable, then you can access it using that property, but it is limited to only that. It is up to you how you use it.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Assisted Solution

by:eozz_2000
eozz_2000 earned 100 total points
ID: 12233075
There are an important difference:

-A property is a value that reflects what is the object, for example: a car has the property "tire"
-A function is a thing that the class do.

This is the difference when you design your application in an object oriented language, if you design your classes map so well, you'll have a design easy to understand.
0
 
LVL 4

Assisted Solution

by:VIkasumit
VIkasumit earned 100 total points
ID: 12234302
Hi..

There are some good suggestion given but I have slightly diferent opinion.

The Classes when introduce has the concept of the Data Member and Data Function. And if you know oops you have read about them. But in VB (not .net) the concept of Property is introduce which combine the two to get you the set of value to your Data Member.

Therefore PROPERTY is a DATA MEMBER.

know what is the difference between the two??

So the answer is : Property is a data member that provides a meaningful name to a data variable within a class. Where as Function is to define the Behaviour of that variable under given condition.

Hence, If you want to have some behaviour change to value you can use the Function and if you just simply want a range restriction and change value use property

VIkasumit
http://www.sumitonnet.20m.com
http://www.vikasgupta.5gigs.com

0
 
LVL 10

Assisted Solution

by:123654789987
123654789987 earned 100 total points
ID: 12235551
A property is used to get-set a value.

In your code above the difference is:

U think the above property as well as function is in a form1. If the value has to be accessed from other forms, if it is a property then it can directly give the stored value. whereas if it is a function it has to do some processing before it returns the value. In form1 u can do this

0
 
LVL 4

Assisted Solution

by:eozz_2000
eozz_2000 earned 100 total points
ID: 12236927
With the properties you can also do some calculations in the get, set parts, you can do some valitations before changing your private value.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question