Solved

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

Posted on 2004-10-05
7
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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