INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Property vs Method?

Property vs Method?

(OP)
Not a problem, more a question.

If you build a class and it has a readonly property, but the value is calculated, would you

A) Put all code in the property
B) Code the property to use a private function
C) Create a public function.

Does it really matter how you expose the value?

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Property vs Method?

You may want to look at what MS suggests
https://msdn.microsoft.com/en-us/library/bzwdh01d(...
https://msdn.microsoft.com/en-us/library/bzwdh01d(...

In general
•Use a property when the member is a logical data member.
•Use a method when: •The operation is a conversion, such as Object.ToString.
•The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
•Obtaining a property value using the get accessor would have an observable side effect.
•Calling the member twice in succession produces different results.
•The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
•The member is static but returns a value that can be changed.
•The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code. In the following code example, each call to the Methods property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.

So that is the differences between A and C. The only difference between A and B is organizational. I like to use a private function within the class and have the property call the function. I find there are times when I want to reuse that function from somewhere else in the class.

RE: Property vs Method?

(OP)
Thanks Majp.

Quote:

I like to use a private function within the class and have the property call the function.

That's exactly how I implemented it...

CODE

' Case Location
Public Property Get Location() As String
    Location = CaseLocation()
End Property 

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Property vs Method?

So if that calculation for location was very expensive

Quote:

The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
Then I guess you may want to consider a public function.

RE: Property vs Method?

I think what they are saying if it was a function it would be more likely that the user saves the return value to a variable, instead of calling the property repeatedly. Does not really make the code any more efficient.

Quote:

is expensive enough that you want to communicate to the user that they should consider caching the result

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close