Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Property or Function?

1 view
Skip to first unread message

Matt Thompson

unread,
Jul 11, 2003, 7:46:37 AM7/11/03
to
Hey,
Whats the difference between having a function that returns a piece of data
(a string or a structure) and a property that does the same? I don't mean
just return a private variable, some work has to be done to the variable.
Just wondering...
Matt


Armin Zingler

unread,
Jul 11, 2003, 8:01:43 AM7/11/03
to
"Matt Thompson" <matt.t...@sensonics.co.uk> schrieb

- Calling a property twice should always returns the same value. Of course,
the value might change between first and second time, but not *due to
calling the property*. That's not true for functions.
- Retrieving a property should be "fast" (however you define "fast").

Always imagine watching the object in the local/watch window: Neither the
property value should change just by watching it, nor it should take 5 min.
to watch it (and lock the IDE).

Armin

Herfried K. Wagner

unread,
Jul 11, 2003, 8:31:54 AM7/11/03
to
Hello,

"Matt Thompson" <matt.t...@sensonics.co.uk> schrieb:


> Whats the difference between having a function that returns
> a piece of data (a string or a structure) and a property that
> does the same?

Properties are used to model *attributes* of an object, functions are used
to model things, the object can *do*.

Calling Properties should not change the state of the object. A property
should return the same value if you call it twice (without any change on the
object in the meantime). Nevertheless, there are some cases where the value
can be different (current time, amount of data processed and so on).

If the calculation for the property value requires a lot of time, it's often
better to define a function, for example GetNumberOfClients.

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


Matt Thompson

unread,
Jul 11, 2003, 8:41:46 AM7/11/03
to
Thanks (Herfield and Armin),
Both yourself and Armin mentioned time as a factor, does a propertry get
treated differently that means it's slower?
Regards,
Matt

"Herfried K. Wagner" <aon.912666908.N.O.S.P.@.M.aon.at> wrote in message
news:eSki9h6R...@TK2MSFTNGP11.phx.gbl...

Herfried K. Wagner

unread,
Jul 11, 2003, 8:45:36 AM7/11/03
to
Hello,

"Matt Thompson" <matt.t...@sensonics.co.uk> schrieb:


> Thanks (Herfield and Armin),
> Both yourself and Armin mentioned time as a factor, does a propertry get
> treated differently that means it's slower?

I expect a property to return the value quickly. There is no performance
difference between properties and functions. Nevertheless, I think the most
important point is that properties are used to model the object's
*attributes*.

Karl Seguin

unread,
Jul 11, 2003, 8:52:20 AM7/11/03
to
The reason for the time factor, as I understand it, is for people using your
object. It's assumed that when you call a property, it will run relatively
fast (maybe do some error checking and return a private _field). If,
however, your property does a lot of stuff and thus runs slow, but people
using your object expect it to run fast (because it's a property), they
might over-use it and get a performance penalty.

You'll definetly want to look at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconpropertyusageguidelines.asp

Karl

"Matt Thompson" <matt.t...@sensonics.co.uk> wrote in message
news:%23u56xl6...@tk2msftngp13.phx.gbl...

Armin Zingler

unread,
Jul 11, 2003, 9:18:22 AM7/11/03
to
"Matt Thompson" <matt.t...@sensonics.co.uk> schrieb

> Thanks (Herfield and Armin),
> Both yourself and Armin mentioned time as a factor, does a propertry
> get treated differently that means it's slower?

No, it is not slower, but if the code takes longer to execute, you should
put it into a function instead of a property.

Armin

Matt Thompson

unread,
Jul 11, 2003, 9:47:53 AM7/11/03
to
Thanks for the link, that's very useful.
Matt

"Karl Seguin" <kseguin##crea.ca> wrote in message
news:%237yUeu6...@TK2MSFTNGP10.phx.gbl...

0 new messages