Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?
Because they're more readable, and because tools which are designed to
use properties will find properties but not accessor methods. Accessor
methods are basically just following a convention - properties also
follow a convention, but have extra metadata declaring them to be
properties.
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
Because it's a high-level, built-in feature designed explicitly for this
purpose and therefore much cleaner IMO. Not only do they instantly convey
their intended purpose to other readers (improving your code's legibility),
but they're also specifically recognized by different tools, controls, etc.
For instance, you can pass an object to the native "PropertyGr id" control
and it will automatically populate the control with all properties in your
object.
On Feb 1, 8:34 am, "Tim Sprout" <t...@ptialaska .netwrote:
Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?
>
-Tim Sprout
In colloquial terms, someone hands you a screwdriver. Do you say, "Why
should I use this, when a butter knife can get the job done?"
Properties formalize an informal standard. In essence, the compiler
now supports and enforces rules about something that (in Java, for
example) used to be the programmer's responsibility to get right. I
can't see any downside to that, although I do remember one poster here
who vehemently maintained that properties were crap and that getter
and setter methods were the only way to go. I can't recall why, sorry.
This may be a stupid question but am I using (see below) Properties or
Accessor methods? If am using Accessor methods, how do I convert the below
to Properties?
Thanks
Dan
public int ObjectID
{
get
{
return objectID;
}
set
{
objectID = value;
}
}
"Tim Sprout" <tman@ptialaska .netwrote in message
news:12s45hk25m q5gdb@corp.supe rnews.com...
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
>
>
-Tim Sprout
>
>
This may be a stupid question but am I using (see below) Properties or
Accessor methods? If am using Accessor methods, how do I convert the below
to Properties?
That's a property. It generates accessor methods behind the scenes, but
it's a property. If you wrote "plain" accessor methods, it would be
something like:
public int GetObjectID()
{
return objectID;
}
public void SetObjectID(int value)
{
objectID = value;
}
The generated accessors have an extra _ in the name, as well as more
metadata.
>This may be a stupid question but am I using (see below) Properties or
>Accessor methods? If am using Accessor methods, how do I convert the
>below
>to Properties?
>
That's a property. It generates accessor methods behind the scenes, but
it's a property. If you wrote "plain" accessor methods, it would be
something like:
>
public int GetObjectID()
{
return objectID;
}
>
public void SetObjectID(int value)
{
objectID = value;
}
>
The generated accessors have an extra _ in the name, as well as more
metadata.
>
--
Jon Skeet - <skeet@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
IMHO, I would - and this is what I do most of the time - is to use
properties. Why? I am a crazy "dynamic business rules" kind of person. When
using reflection, for instance, properties are your friend.
Also, properties are more readable than accessor methods, IMO.
Comment