By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,409 Members | 1,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,409 IT Pros & Developers. It's quick & easy.

accessing member variables directly vs property accessor

P: n/a
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
May 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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
"TS" <ma**********@nospam.nospam> wrote in message
news:OE**************@TK2MSFTNGP05.phx.gbl... 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

May 10 '06 #2

P: n/a
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
May 10 '06 #3

P: n/a
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.)

May 11 '06 #4

P: n/a
TS
THANKS

"Kevin Yu [MSFT]" wrote:
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.)

May 12 '06 #5

P: n/a
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.)

May 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.