473,382 Members | 1,431 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,382 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 1790
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.