473,401 Members | 2,068 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,401 software developers and data experts.

Use properties or variables in class

I have a class as:

public class a :
{
private float m_b;

public float b
{
get
{
return m_b;
}
set
{
m_b = value;
}

public float compute()
{
//************************************************** ***
//This method
return m_b * 369871;
//************************************************** ***
//Or this method
return b * 369871;
//************************************************** ***
}
}

What are peoples thoughts on the above.
Personally I prefer to use the member var instead of the property because it
removes 1 step but in some cases there is validation that needs to be done
or other so I might need to use the property. Or in some cases a property
does not have a member var.

Interested to know other peoples thoughts.
Cheers
JB

Nov 16 '05 #1
3 1793
John,

I would recommned using the property to gain access to the member
variable, especially since it is a read/write property. If the logic should
change regarding how the float returned by the b property is changed then
you will only have to modify your code in one place. And if you add other
logic (perhaps validate the range of m_b) or add security checks then you
will resuse that code.

HTH,

//Andreas

"John Baro" <jo***@NOSPAMmesware.com.au> skrev i meddelandet
news:DI*******************@news-server.bigpond.net.au...
I have a class as:

public class a :
{
private float m_b;

public float b
{
get
{
return m_b;
}
set
{
m_b = value;
}

public float compute()
{
//************************************************** ***
//This method
return m_b * 369871;
//************************************************** ***
//Or this method
return b * 369871;
//************************************************** ***
}
}

What are peoples thoughts on the above.
Personally I prefer to use the member var instead of the property because it removes 1 step but in some cases there is validation that needs to be done
or other so I might need to use the property. Or in some cases a property
does not have a member var.

Interested to know other peoples thoughts.
Cheers
JB

Nov 16 '05 #2
John Baro <jo***@NOSPAMmesware.com.au> wrote:

<snip>
What are peoples thoughts on the above.
Personally I prefer to use the member var instead of the property because it
removes 1 step but in some cases there is validation that needs to be done
or other so I might need to use the property. Or in some cases a property
does not have a member var.


I would recommend *always* using a property. Benefits of properties:

o The interface can remain consistent if the implementation changes
(e.g. to get the data from elsewhere, or compute it, or store it
in multiple variables)

o You can have a property which is read-only to the outside world,
but can be altered within your code (eg ArrayList.Count)

o From Whidbey onwards (and I don't know why it wasn't in to start
with) you will be able to have a property with different access
modifiers for set and get (at which point I'd recommend *only*
accessing the underlying variable from the property)

o You can easily add tracing or breakpoints to see when a value is used

o You can do validation etc

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
John Baro <jo***@NOSPAMmesware.com.au> wrote:

<snip>
What are peoples thoughts on the above.
Personally I prefer to use the member var instead of the property because
it
removes 1 step but in some cases there is validation that needs to be
done
or other so I might need to use the property. Or in some cases a property
does not have a member var.
I would recommend *always* using a property. Benefits of properties:


I would recommend it *almost* always(say 98-99%+ of the time). There are a
few situations where the property code really is a hassle. The most obvious
is marshalled structures, but other internal situations may permit using a
field for the sake of clarity.

Clarity is the point here. If the class doesn't need to consider versioning
and the class and developer would benifit from a clear, non cluttered view
of members, then simple properties aren't worth it. If there are a large
number of members and the class is little more than a datastore, then simple
properties are also probably not worth it.

While I may not go as far as Gunnerson does in his blog[1], I do think there
are cases where not bothering with the property outside of versioning
boundries is an acceptable approach. I would say that simple properties
should be used on all public, protected, and internal data members on public
and internal classes and on public structures, with the exception of nested
classes\structures and other internal classes\structures which are tightly
bound to a specific class. I would probably argue for public fields when you
are working with structures that exist for the purposes of 1) PInvoke or COM
Interop, or 2) Simple remoting\message passing. Such structures, IIMHO,
should have no behaviour and properties are a waste of time. They are
messages or interop middlemen and should be as simple as possible. I am
obviously not speaking about complex properties here(like using properties
to provide a simple interface to a bitfield or do validation, what have
you), I just mean simple get\set properties which do nothing but mask a
variable in a place where you'll likely never need validation or you can't
do validation and the code will be compiled with all the code that can
access it at all times.

With the coming of InternalsAccessableToAttribute(Or whatever they are
calling it), internals potentially become versioning boundaries as well, so
use of properties on internal classes that may be used is a must.

This would be entirely moot if the language designers had considered the
prevalence of simple get\set properties and had provided property
declaration syntax as or nearly as simple as a field declaration or even if
the designers had chosen to completely eliminate fields(Perhaps that would
have been the right choice itself).

1. http://blogs.msdn.com/ericgu/archive.../12/52836.aspx

o The interface can remain consistent if the implementation changes
(e.g. to get the data from elsewhere, or compute it, or store it
in multiple variables)

o You can have a property which is read-only to the outside world,
but can be altered within your code (eg ArrayList.Count)

o From Whidbey onwards (and I don't know why it wasn't in to start
with) you will be able to have a property with different access
modifiers for set and get (at which point I'd recommend *only*
accessing the underlying variable from the property)

o You can easily add tracing or breakpoints to see when a value is used

o You can do validation etc

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: julien | last post by:
Hello, I don't know when to use fields and when to used properties. It looks to me that using properties is always better. But I guess fields must be better in some cases, otherwise they wouldn't...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
6
by: Charles D Hixson | last post by:
I'm trying to construct read-only variables at the class level. I've been unsuccessful. Any suggestions? Mixing @classmethod and @property doesn't appear to produce workable code. Ditto for...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.