?
Solved

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

Posted on 2004-10-05
7
Medium Priority
?
216 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 400 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 400 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Assisted Solution

by:eozz_2000
eozz_2000 earned 400 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 400 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 400 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

771 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