473,804 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Properties vs Get Set Accessor methods

Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?
-Tim Sprout
Feb 1 '07 #1
8 8243
Tim Sprout <tm**@ptialaska .netwrote:
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.

--
Jon Skeet - <sk***@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
Feb 1 '07 #2
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.
Feb 1 '07 #3
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.

Feb 1 '07 #4
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" <tm**@ptialaska .netwrote in message
news:12******** *****@corp.supe rnews.com...
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout


Feb 1 '07 #5
Dan Reber <dr****@nospam. comwrote:
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 - <sk***@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
Feb 1 '07 #6
Great, thanks.

Dan

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Dan Reber <dr****@nospam. comwrote:
>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 - <sk***@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

Feb 1 '07 #7
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.

Again, it is a personal opinion.

--
Regards,
Robson Siqueira
Enterprise Architect
"Tim Sprout" <tm**@ptialaska .netwrote in message
news:12******** *****@corp.supe rnews.com...
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout


Feb 1 '07 #8
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout
Great responses. Thanks.

-Tim Sprout

Feb 2 '07 #9

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

Similar topics

10
3792
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic properties. Something similar to a java hash table. One can add new properties at runtime and remove them as well. I have a really fancy third party property inspector available to me that I would very much like to reuse. This inspector uses...
23
391
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
1
437
by: SpookyET | last post by:
I have thought that you can disable a set or a get when you override a virtual property. I guess that I was wrong. The code below compiles and doesn't throw the exceptions that I have expected. It calls the base accessor. using System; namespace Test { public abstract class MetaDataFile
9
1706
by: keithv | last post by:
I'm looking at a bunch of code which has lots of class fields converted into properties with just the simplest code: private int empID; public int empID { get { return empID; } set { empID = value; } } My question is, isn't this useless and inefficient?
10
1872
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...
11
1798
by: john andrew | last post by:
-- hello When using properties with OOP, VB6 had Get/Let and VB.net has Readonly/and .... Are theses OOP concepts with Property use or only specific to Visual Basic properties, With this property use does C++ have this as well or just Visual C++.?
11
2555
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....
11
6807
by: Kondapanaidu | last post by:
Hi, What is the difference between Properties and functions. Why dont we go for functions instead of properties. Regrads
20
2345
by: Artur Siekielski | last post by:
Hi. I would like to have declarative properties in Python, ie. something like slots definitions in defclass in Common Lisp. It seems that even Java will have it, using a library ( https://bean-properties.dev.java.net/ ). I know about 'property' function in Python, but it's normal usage isn't declarative, because I have to code imperatively getters and setters:
0
9714
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
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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
10351
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7638
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
6866
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
5534
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...
3
3002
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.