473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1813
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***@NOSPAMme sware.com.au> skrev i meddelandet
news:DI******** ***********@new s-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***@NOSPAMme sware.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
John Baro <jo***@NOSPAMme sware.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\structu res and other internal classes\structu res 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\messag e 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 InternalsAccess ableToAttribute (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.co m>
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
1765
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 exist! Thank you Julien
10
1871
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 starting my new one. In that time, I have decided to learn C#. I picked up the book, "Programming C# (4th Edition)" recently and have read most of it. I've written about 1,500 lines of C# now, and I've run into the first really ugly thing I...
8
2745
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 expose the variables or is it OK to just access the variables directly? Keep in mind that I am talking about accessing the variables from within the class that they are defined. Thanks!
11
2552
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 well as learning C#, I think that it's way overdue for me to start learning C++ Templates (I've been learning it for about 5 years now). I think that adding this type of functionality would be a good exercise to help learn template programming....
6
1275
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 mixing @classmethod and __getattr__. (The property approach compiles, but execution says that you can't execute properties.) I've got a rather large number of variables, so I don't want to define function accessors for each of them, and I...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.