Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Property vs Method? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
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
 
You may want to look at what MS suggests

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

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
 
So if that calculation for location was very expensive
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.
 
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.
is expensive enough that you want to communicate to the user that they should consider caching the result
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top