accessing member variables directly vs property accessor

T

TS

is it preferred to access member variables directly in code, on the page
that declared them, versus going thru a property accessor? I would think
that since theres no security concerns or anything it would be more
efficient to access directly. I also don't think that you would need to do
any special validation since the class developer knows what the rules are.

Note, i am not referring to accessing member variables declared in a
different class.

any comments appreciated

Thanks
 
L

Lee Alexander

is it preferred to access member variables directly in code, on the page
that declared them, versus going thru a property accessor?
Depends on what your doing... I would use a property accessor from within
it's class if that property does or will do something non-trivial, like for
instance late creation of an object or possibily validating paramter values
etc...
it would be more efficient to access directly
Thi isn't necessarily the case. When run in release mode outside of the IDE
the Jitter will inline code so the property execution speed "overhead" could
be factored out.

Regards
Lee
 
M

Michael Nemtsev

Hello TS,

Using properties instead of fields is more natural way. You don't need to
gave direct access to your fields, all fields shoul be private/protected.
Look at any .NET coding style (for exaple Microsoft one on the msdn)

Properties are more secure and elegant way, because u can easily control
access to your fields

T> is it preferred to access member variables directly in code, on the
T> page that declared them, versus going thru a property accessor? I
T> would think that since theres no security concerns or anything it
T> would be more efficient to access directly. I also don't think that
T> you would need to do any special validation since the class developer
T> knows what the rules are.
T>
T> Note, i am not referring to accessing member variables declared in a
T> different class.
T>
T> any comments appreciated
T>
T> Thanks
T>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
K

Kevin Yu [MSFT]

Hi TS,

It's better to create properties to access the member variables. As your
design might be changed in the future, you'll have more flexibility on
controlling the access to those variables. Less code need to be modified to
change the access to the variables.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Yu [MSFT]

You're welcome!

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top