Solved

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

Posted on 2004-10-05
7
209 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connect to a database from Excel using JDBC instead of ODBC 3 47
LINQ - C# to VB convertion 12 46
Help with Syntax 9 26
Get the selected ValueMember of Combobox 5 19
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

21 Experts available now in Live!

Get 1:1 Help Now