|
-
Sep 27th, 2005, 01:14 PM
#1
differences in property/function
Is there any REAL difference between the following 2 items other than syntax?
VB Code:
Public Function GetString() as String
return "hello world"
End Function
Public Readonly Property GetString() as String
return "hello world"
End Property
-
Sep 27th, 2005, 01:21 PM
#2
Re: differences in property/function
Nothing, I think. They probably compile to the same IL.
"Property" is only there so you can pair up get/set clauses. I suppose it encourages clear coding style.
-
Sep 27th, 2005, 01:23 PM
#3
Fanatic Member
Re: differences in property/function
I think I read somewhere that you use Functions when the method does some operations like looking through an array, do something and then expose the result and you use properties when you just want to expose a variable. I must admit through that I am sometimes tempted to use functions, just like in Java where you have GetXXX and SetXXX functions. Actually I think the compiler actually generates Get/Set functions behind the scene.
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Sep 27th, 2005, 01:26 PM
#4
Re: differences in property/function
 Originally Posted by wossname
Nothing, I think. They probably compile to the same IL.
"Property" is only there so you can pair up get/set clauses. I suppose it encourages clear coding style.
That is what I figured.. I just wasn't sure if there was any clear reason to use one or the other...
I will usually use functions for the type of example I posted...
-
Sep 27th, 2005, 01:35 PM
#5
Re: differences in property/function
Probably, the only real advantage for a property would be the ability to use a single function for Get and Set rather than two separate ones. In the case of ReadOnly, it may not make a difference.
The one way that it could make a difference would be if you had a variable that was exposed through a property (as opposed to a string literal like you used in your example). I have no idea whether or not this is done, but it would be possible that the property would execute faster if it returned an offset into the class to return the item, rather than incurring the overhead of a function call. That kind of optimization is probably not done in .NET, but I suppose it could be.
My usual boring signature: Nothing
 
-
Sep 27th, 2005, 04:48 PM
#6
Re: differences in property/function
You can databind to the property but not the function/method.
-
Sep 27th, 2005, 04:52 PM
#7
Re: differences in property/function
you and your love for databinding!!!! 
good point though...
so basically in SOME situations.. there are a few fundamental differences.. but for most cases they are pretty much the same thing... something in a class that returns a datatype.
-
Sep 27th, 2005, 05:53 PM
#8
Re: differences in property/function
True enough.
On the other hand, you have to consider maintenance. If you declare something as a property, it seems to suggest a type of behavior. Though you could do the same thing with functions, are you telling a third party the same thing with a method rather than a property.
Actually, what I just said makes a bit of sense to me, but not all that much. I can't support it further than that, but it MAY be a consideration.
My usual boring signature: Nothing
 
-
Sep 27th, 2005, 05:59 PM
#9
Re: differences in property/function
no i get what you are saying, because something that is a function you KNOW is returning a value, but when its a property, you need to dig a little bit into the help or check the intellisense, (OR get a syntax error) to know that the property you are tring to assigning a value to is read only...
-
Sep 27th, 2005, 06:10 PM
#10
Re: differences in property/function
A property is supposed to represent some inherent attribute of an object, while a method is supposed to represent some behaviour of that object. The lines do become blurry at times, but if, from the outside, something looks like it could be represented by a simple variable then it should generally be a property. Look at the example of the Form.Visible property and the Form.Show and Form.Hide methods. They have the same result but one represents an attribute inherent to the object and the other represents a behaviour.
-
Sep 27th, 2005, 06:15 PM
#11
Re: differences in property/function
 Originally Posted by jmcilhinney
A property is supposed to represent some inherent attribute of an object, while a method is supposed to represent some behaviour of that object. The lines do become blurry at times, but if, from the outside, something looks like it could be represented by a simple variable then it should generally be a property. Look at the example of the Form.Visible property and the Form.Show and Form.Hide methods. They have the same result but one represents an attribute inherent to the object and the other represents a behaviour.
I agree, and I generally have a property be something that a user would expect to be a feature of the class, whereas a function is something that a user would expect to be a product of the class.
Behind the scenes, some of the properties do some fairly extensive calculations, but they still are things that seem like they ought to be features.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|