471,355 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Accessing private attributes from within classes

In the classes I develop my attributes are always private and are
exposed using properties.
>From within the class is more correct to access the attributes
directly or to access the attributes using the properties?
Does "wrapper" setter/getter properties introduce time overhead in
execution?

Thanks,
Matteo

Jun 14 '07 #1
3 1959
On Jun 14, 2:24 pm, Beorne <matteo...@gmail.comwrote:
In the classes I develop my attributes are always private and are
exposed using properties. From within the class is more correct
to access the attributes directly or to access the attributes using
the properties?
Does "wrapper" setter/getter properties introduce time overhead in
execution?
(I'll assume you mean fields or instance variables when you say
attributes - attributes are something else in .NET/C#.)

If your properties are "straight-through" ones which do absolutely
nothing, there shouldn't be any performance hit.

As for whether you should use the fields directly, that depends on
what you're doing. I personally like to do that as it means that if I
add validation at a later date, my private changes will still be
validated. However, there are times you expressly *don't* want to do
this. For instance, suppose you had two fields, first and last, and
your properties made sure that first was never bigger than last. You
might have a SetFirstAndLast method to set them both at the same time,
which would reasonably use the fields directly (or field then
property) to avoid difficulties where setting either property first
would, in some cases, fail validation.

Jon

Jun 14 '07 #2
Hi,
"Beorne" <ma*******@gmail.comwrote in message
news:11*********************@a26g2000pre.googlegro ups.com...
In the classes I develop my attributes are always private and are
exposed using properties.
>>From within the class is more correct to access the attributes
directly or to access the attributes using the properties?
Does "wrapper" setter/getter properties introduce time overhead in
execution?
If they are just a way to access the internal member you can use it.
Personally I use the Setter method to assign values, it clears the situation
when I add some code later on (either validation, or events). I do not have
that much care when getting the value though. sometimes I nse the member and
some other the property.

Regarding performance problems I would not think about that. Both the
compiler and the JIT compiler will take care of it.

Jun 14 '07 #3
>
(I'll assume you mean fields or instance variables when you say
attributes - attributes are something else in .NET/C#.)
Yes, sorry about the bad naming.

Thank you both very much!.

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Borek | last post: by
5 posts views Thread by Dennis | last post: by
1 post views Thread by John Dann | last post: by
3 posts views Thread by redefined.horizons | last post: by
6 posts views Thread by earthwormgaz | last post: by
63 posts views Thread by time.swift | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.