473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2025
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Borek | last post by:
In my project I have to access in one business method of session bean usually more then 10 CMP Beans. I would like to have some utils classes, which could get me right instance of CMP or create...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
6
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
5
by: Dennis | last post by:
How do I access a private property of a class from outside the class. I have a class that has a property which I want to access in my code for read/write but allow end users to only read. --...
1
by: John Dann | last post by:
I'm realising that there's something important that I don't understand about acccessing class properties. In fact I'm not even sure that I can explain clearly what it is that I don't understand!...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.