473,382 Members | 1,362 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.

Attributes and Properties

Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?
Nov 17 '05 #1
8 1692
JV
If you just make your integer public, you lose any/all control over who
accesses it and how. This may or may not ever truly matter in the long run.
All depends.

If you wrap it in a property, you will always be able to address problems
down the road if/when they occur. My advice is just wrap that sucker. It's
not much work and you may someday be REALLY REALLY glad you did.

And, of course, for certain situations that is your only option...for
example, when writing a server control for asp.net, or creating an object
that may be data bound.
"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?

Nov 17 '05 #2
>From an OOP perspective, there are some practical considerations that
would make properties "safer" in the long term. For example, from a
pure OOP point of view, all classes should be accessed through
interfaces, and an interface can only define properties and methods,
not fields.

There is one consideration that should be taken into account: data
binding (at least in ASP.NET) only work with properties, not with
fields.

Nov 17 '05 #3
One reason I can think of is if your class could be inherited (not sealed)
and you wanted to allow a property to have different implementation. Your
public field doesn't lend itself to polymorphism as greatly as an actual
property of a class.

Another instance would be if your property is defined in an interface.
Again, a class that uses your interface will want to have implementation code
for said property.

Bear in mind, not all properties are as simple as the one you used in your
example. Some need to execute code to format, perform conversions, or do some
other task before assigning or returning the value of the property.

At any rate, it's a good OOP habit to get into regardless of the
implementation of the property, be it complex or simple as your example.

-Demetri

"Chuck Bowling" wrote:
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?

Nov 17 '05 #4
In the example quoted, the only value is that access to the property would
be made with:

<object>.MyInt

whereas if the private member were public the access to it would be made
with:

<object>.myInt

Apart from that I see no value.

If you wanted to use a specific name for a member internally with the class,
but expose it with a completely different name then there would be value.

private int myInt;

public int YourInt
{
get { return myInt; }
etcetera

In many cases there is more processing to a property get clause than a
simple return and simalarly more proccessing than a simple assignment to a
property set clause.
"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?

Nov 17 '05 #5
There are several advantages of using properties over data members:

o Only properties can be used directly with databinding.
o When later, you need to make access to a data member thread safe, it is
easy to add for example a lock(this) to the get/sets.
o from the OOP standpoint you can inherit from a class and override the
property get/sets
o properties allow you to later add validation rules to the set methods
without having to find all locations where a public data member is accessed.
Imaging all other actions you might want to later take when
someone/something gets/set your field: logging of that activity, triggering
some other events, assisting debugging by setting a breakpoint in the
get/sets, etc...
o If you think you can always later come back and change the public data
member to a property, you will have to recompile all other assemblies that
use your class that once had the public field. That is due to public fields
and properties not being binary compatible since properties are functions
whereas public fields are direct access.

The work to hack in the get/sets will become a right click and select from
the Refactor menu to wrap the private field. Once you develop the habit of
creating properties where you once used only public fields, it really takes
no time.

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?

Nov 17 '05 #6
To JV, Mohammad, Demetri, Stephany Young, and Mr. Sentgerath; Thank you all
for the responses. So good food for thought.

FWIW, I usually wrap data members in Properties but until now I really
didn't have a good idea why I did it - only that I had read somewhere that
it was a good idea. I was aware that Properties allowed additional
processing on a member but beyond that wasn't clear on what other utility
they provided. Thanks.

"Robert Sentgerath" <rs*********@landstar.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
There are several advantages of using properties over data members:

o Only properties can be used directly with databinding.
o When later, you need to make access to a data member thread safe, it is
easy to add for example a lock(this) to the get/sets.
o from the OOP standpoint you can inherit from a class and override the
property get/sets
o properties allow you to later add validation rules to the set methods
without having to find all locations where a public data member is
accessed.
Imaging all other actions you might want to later take when
someone/something gets/set your field: logging of that activity,
triggering
some other events, assisting debugging by setting a breakpoint in the
get/sets, etc...
o If you think you can always later come back and change the public data
member to a property, you will have to recompile all other assemblies that
use your class that once had the public field. That is due to public
fields
and properties not being binary compatible since properties are functions
whereas public fields are direct access.

The work to hack in the get/sets will become a right click and select from
the Refactor menu to wrap the private field. Once you develop the habit of
creating properties where you once used only public fields, it really
takes
no time.

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?


Nov 17 '05 #7
There are several advantages of using properties over data members:

o Only properties can be used directly with databinding.
o When later, you need to make access to a data member thread safe, it is
easy to add for example a lock(this) to the get/sets.
o from the OOP standpoint you can inherit from a class and override the
property get/sets
o properties allow you to later add validation rules to the set methods
without having to find all locations where a public data member is accessed.
Imaging all other actions you might want to later take when
someone/something gets/set your field: logging of that activity, triggering
some other events, assisting debugging by setting a breakpoint in the
get/sets, etc...
o If you think you can always later come back and change the public data
member to a property, you will have to recompile all other assemblies that
use your class that once had the public field. That is due to public fields
and properties not being binary compatible since properties are functions
whereas public fields are direct access.

The work to hack in the get/sets will become a right click and select from
the Refactor menu to wrap the private field. Once you develop the habit of
creating properties where you once used only public fields, it really takes
no time.

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?

Nov 17 '05 #8
To JV, Mohammad, Demetri, Stephany Young, and Mr. Sentgerath; Thank you all
for the responses. So good food for thought.

FWIW, I usually wrap data members in Properties but until now I really
didn't have a good idea why I did it - only that I had read somewhere that
it was a good idea. I was aware that Properties allowed additional
processing on a member but beyond that wasn't clear on what other utility
they provided. Thanks.

"Robert Sentgerath" <rs*********@landstar.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
There are several advantages of using properties over data members:

o Only properties can be used directly with databinding.
o When later, you need to make access to a data member thread safe, it is
easy to add for example a lock(this) to the get/sets.
o from the OOP standpoint you can inherit from a class and override the
property get/sets
o properties allow you to later add validation rules to the set methods
without having to find all locations where a public data member is
accessed.
Imaging all other actions you might want to later take when
someone/something gets/set your field: logging of that activity,
triggering
some other events, assisting debugging by setting a breakpoint in the
get/sets, etc...
o If you think you can always later come back and change the public data
member to a property, you will have to recompile all other assemblies that
use your class that once had the public field. That is due to public
fields
and properties not being binary compatible since properties are functions
whereas public fields are direct access.

The work to hack in the get/sets will become a right click and select from
the Refactor menu to wrap the private field. Once you develop the habit of
creating properties where you once used only public fields, it really
takes
no time.

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:ev****************@tk2msftngp13.phx.gbl...
Is there any justification - from an OOP perspective - for wrapping an
attribute in a Property beyond the ability to restrict access?

private int myInt;

public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
Is there any value in the above as opposed to simply making myInt public?


Nov 17 '05 #9

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

Similar topics

6
by: beliavsky | last post by:
I have started using classes with Python and have a question about their use. In Python, 'attributes are all "public" and "virtual" in C++ terms; they're all accessible everywhere and all looked...
5
by: Flipje | last post by:
In my view, there is a major drawback to using attributes: the getter and the setter have identical protection levels. But I usually want the getter to be public and the setter to be protected or...
5
by: WindAndWaves | last post by:
Greetings once more, I was wondering if someone figured out the difference between attributes and properties. I noticed that things like fields have a number of properties, but also...
12
by: Pol Bawin | last post by:
Hi All, Did somebody already define attributes for numerical properties to define value : minima, maxima, a number of decimal, ...? This information would be useful to unify syntax Polo
2
by: S.Sigal | last post by:
Hello: I've seen code in some books for databinding that handles attaching Attributes to Properties during runtime...which is very close (but no cigar) to what I am looking for... The code...
13
by: Patrick * | last post by:
Reading a book on .ASP .NET I am getting a bit mixed up as to the difference between "property" and "attribute" as the terms don't seem to be used consistently in the book I am reading. Can anyone...
3
by: vivekian | last post by:
Hi , I am moving on from C++ to C# . Though have understood how attributes work , still dont understand what need do the fullfil ? Where should they be used ? What are their advantages ? some...
2
by: P4trykx | last post by:
Hello I'm want to add some custom attributes to WebControls using WebControl.Attributes.Add("abc","234"); So the html output will look like this, <input type="hidden" abc="123" /etc. I know...
3
by: Beorne | last post by:
In the classes I develop my attributes are always private and are exposed using properties. directly or to access the attributes using the properties? Does "wrapper" setter/getter properties...
7
by: bstieve | last post by:
Hi all, i'm looking at the next problem. i'm trying to get the names of attributes of object or of types. example class test private name as string sub new (byval test as string) me.name =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
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
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...

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.