Results 1 to 11 of 11

Thread: differences in property/function

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    differences in property/function

    Is there any REAL difference between the following 2 items other than syntax?
    VB Code:
    1. Public Function GetString() as String
    2.   return "hello world"
    3. End Function
    4.  
    5. Public Readonly Property GetString() as String
    6.   return "hello world"
    7. End Property

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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 ...

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: differences in property/function

    Quote 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...

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: differences in property/function

    You can databind to the property but not the function/method.

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: differences in property/function

    Quote 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
  •  



Click Here to Expand Forum to Full Width