Solved

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

Posted on 2004-10-05
7
208 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now