Best Practice - Accessing Properties In Objects? 
Author Message
 Best Practice - Accessing Properties In Objects?

Which is best practice for access the value of a property in an object?

1. Using Me.PropertyName to access the value through the Get property

OR

2. Accessing the private variable that stores the value of the property directly

Thanks
William D. Bartholomew



Wed, 29 Dec 2004 09:21:51 GMT  
 Best Practice - Accessing Properties In Objects?


Quote:
> Which is best practice for access the value of a property in an object?

> 1. Using Me.PropertyName to access the value through the Get property

> OR

> 2. Accessing the private variable that stores the value of the property

directly

Definitely the property name.  Part of the whole point of OOP is
encapsulation.  You are hiding the internal variables from outside
influence.  The property routines can do error checking, make adjustments,
etc.  An exposed variable (it would have to be public, not private) can
simply be changed -- but you have no way to know how or why it is changed.

Marty



Wed, 29 Dec 2004 10:15:44 GMT  
 Best Practice - Accessing Properties In Objects?


Quote:



>> Which is best practice for access the value of a property in an
>> object?

>> 1. Using Me.PropertyName to access the value through the Get property

>> OR

>> 2. Accessing the private variable that stores the value of the
>> property
> directly

> Definitely the property name.  Part of the whole point of OOP is
> encapsulation.  You are hiding the internal variables from outside
> influence.  The property routines can do error checking, make
> adjustments, etc.  An exposed variable (it would have to be public,
> not private) can simply be changed -- but you have no way to know how
> or why it is changed.

> Marty

Yes, sorry, I didn't word my question clearly enough, I was wondering what is the best way to obtain the
value of the property from WITHIN the object...

Thanks
William



Wed, 29 Dec 2004 11:51:59 GMT  
 Best Practice - Accessing Properties In Objects?


Quote:




> >> Which is best practice for access the value of a property in an
> >> object?

> >> 1. Using Me.PropertyName to access the value through the Get property

> >> OR

> >> 2. Accessing the private variable that stores the value of the
> >> property
> > directly

> > Definitely the property name.  Part of the whole point of OOP is
> > encapsulation.  You are hiding the internal variables from outside
> > influence.  The property routines can do error checking, make
> > adjustments, etc.  An exposed variable (it would have to be public,
> > not private) can simply be changed -- but you have no way to know how
> > or why it is changed.

> > Marty

> Yes, sorry, I didn't word my question clearly enough, I was wondering what

is the best way to obtain the

Quote:
> value of the property from WITHIN the object...

> Thanks
> William

Probably still using Me.PropertyName. However, there doesn't seem to be any
consensus on this when you see published code. And I'm not consistent
myself!


Wed, 29 Dec 2004 19:24:50 GMT  
 Best Practice - Accessing Properties In Objects?
William:

Quote:
> Yes, sorry, I didn't word my question clearly enough, I was wondering what is the best way to obtain the
> value of the property from WITHIN the object...

I still use Me.PropertyName. Even tho right now that Property equates to
a private variable, at some point in the future the Let and/or Get might
have code added that you would almost certainly want executed no matter
who accesses that property (validation and tracing come to mind
quickly). It also makes searching for references to that property easier
-- there's just one string to search for -- the Property-name -- you
don't need a second search to find the private variable also.

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982



Wed, 29 Dec 2004 19:50:29 GMT  
 Best Practice - Accessing Properties In Objects?


Quote:

> Yes, sorry, I didn't word my question clearly enough, I was wondering what

is the best way to obtain the

Quote:
> value of the property from WITHIN the object...

As soon as I sent my reply, it hit me that might be what you meant.

There is less consensus there -- I see it done both ways and have heard
arguments both ways and am still not wholly convinced either way.

By using the Me.PropertyName, you make it easy to spot the references and
any code that is going into your property routines (error checking, etc.)
can be executed even inside the object instance.

OTOH, the point of the property is for outside reference and these are
variables internal to the instance of the class.  If you use a naming
convention that makes instance variables stand out, you might not really
need it.  Also, since you are doing indirection, there has to be a *very*
slight performance affect.

I used to lean towards #2, but am becoming more convinced that #1 might be
better -- especially after writing some rather large classes where it
becomes more difficult to keep the names obvious.

Marty



Wed, 29 Dec 2004 21:42:41 GMT  
 Best Practice - Accessing Properties In Objects?
Thanks Kevin, Lee and Marty,

I've been tending towards the Me.PropertyName for consistency and reducing code and from your
responses it does look like the way to do. And I agree, there seems to be NO consensus out there...

Cheers
William



Thu, 30 Dec 2004 09:44:51 GMT  
 Best Practice - Accessing Properties In Objects?
I agree, also It would be easier to revert back to the internal variable if
performance later became an issue...

Schneider


Quote:
> Thanks Kevin, Lee and Marty,

> I've been tending towards the Me.PropertyName for consistency and reducing
code and from your
> responses it does look like the way to do. And I agree, there seems to be

NO consensus out there...
Quote:

> Cheers
> William



Tue, 04 Jan 2005 12:55:10 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Best Practice: Referring to Public Properties or Private Variables

2. Best practice for data-related business objects

3. best practices with Command object

4. Variants vs. Types in COM objects...best practices

5. Instantiating & Releasing ActiveX EXE objects Best Practices

6. Access Development Best Practices Chat 10/03/02

7. Best Data Access Practice?

8. Data Access Best Practice Question

9. change in join syntax best practices?

10. Good programming practice? - Generic options

11. best practice

12. Good practice question

 

 
Powered by phpBB® Forum Software