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

Home Posts Topics Members FAQ

Use of properties

Hi, i would like to get some view points on the use of properties within
classes. My belief is that properties should generally be used to return
values of private members. They should not do anything that is likely to
return an error and they should not instantiate or return any big objects
etc.

My company seems to be using them for everything which is starting to cause
me concern. Any comments or other viewpoints is appreciated.
Oct 24 '06
39 1953
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>
Well, there are certainly *plenty* of "guidelines " saying not to make
fields public, and plenty of reasons not to. Using fields instead of
properties:

1) You can't do validation
2) You can't *easily* break on all access/modification
3) You can't make a field read-only for everything outside the class
4) You can't change the implementation of how that essential
characteristic of the class is represented internally

If you need to do any of that, then of course you must use properties.
That's a no-brainer.

Otherwise, I don't see the point in writing both getters and setters instead
of making fields public. I think both approaches represent code smells, but
that the latter is more intention-revealing and less messy than the latter.
It's also more agile, in not writing code that's not needed.

///ark
Oct 25 '06 #21
Mark Wilden <mw*****@commun itymtm.comwrote :
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...

Well, there are certainly *plenty* of "guidelines " saying not to make
fields public, and plenty of reasons not to. Using fields instead of
properties:

1) You can't do validation
2) You can't *easily* break on all access/modification
3) You can't make a field read-only for everything outside the class
4) You can't change the implementation of how that essential
characteristic of the class is represented internally

If you need to do any of that, then of course you must use properties.
That's a no-brainer.

Otherwise, I don't see the point in writing both getters and setters instead
of making fields public. I think both approaches represent code smells, but
that the latter is more intention-revealing and less messy than the latter.
It's also more agile, in not writing code that's not needed.
On the other hand, it can cause problem if you *later* want any of the
above - you break both source and binary compatibility.

--
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
Oct 25 '06 #22
What post are you trying to reply to, really?

Bob Jones wrote:
On 2006-10-24 15:54:18 -0500, Göran Andersson <gu***@guffa.co msaid:
>Martijn Mulder wrote:
>>>Hi, i would like to get some view points on the use of properties

Properties are best avoided. They seem great at first, but in fact
they obscure your code. Suddenly there is a third kind of class
member, data, functions (methods) and now, NEW NEW NEW, properties.
My experience is, when I want to get some value, I use Get...(), when
I want to set something, I use Set...(value); Then you know where to
look for what happens.

It seams that you totally missed the entire concept of object
orientation.

".. totally missed the entire concept of [OO]". Not. "Properties " a-la
MS Csharp (a.k.a. "not Java") is an implementation supporting the
concepts of encapsulation and information hiding - which are OO concepts.

I agree that properties can "obscure" the code, but I do not agree that
they are "best avoided." Seems to me they are a somewhat cutesy
"we-gotta-be-different" Microsoft thing that I first saw as part of
Visual Basic's back then "kinda-sorta OO implementation" a few years
ago. Now that MS seems to be making all .NET languages exactly the same,
but different (?!) C sharp gets properties.

Yeah, they're just odd (to Java, C++, etc. programmers) "getters and
setters", and the way we tend to use them - an Uppercase spelling
(Pascal Case) of a private field, they do tend to be confusing at times.
Nonetheless I kinda like the clean synax of it.

I do think they're over-used. There really is no point in making a field
private and supplying a Property when you really want a public field.
Make the field public, duh. There is no rule that says all fields (aka
instance variables) must not be public. OTOH I think if you have a
derived value - that takes some kakulatin' - then a Property is a neat
way to make the code look like you're referencing a public field.
Oct 25 '06 #23
On Tue, 24 Oct 2006 11:35:26 -0700, "Peter Duniho"
<Np*********@Nn OwSlPiAnMk.comw rote:
>To me, properties are nouns and adjectives related to the class, while
methods are the verbs of the class.
That's a good rule of thumb.

--
Posted via a free Usenet account from http://www.teranews.com

Oct 25 '06 #24
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>>
Otherwise, I don't see the point in writing both getters and setters
instead
of making fields public. I think both approaches represent code smells,
but
that the latter is more intention-revealing and less messy than the
latter.
It's also more agile, in not writing code that's not needed.

On the other hand, it can cause problem if you *later* want any of the
above - you break both source and binary compatibility.
If binary compatibility is necessary, then you have to make up-front
decisions that would otherwise be better deferred. That's not the case for
most code, at least in my experience.

Would the source compatibility part apply only to ref arguments?

///ark
Oct 26 '06 #25
Paul Mcilreavy wrote:
Hi, i would like to get some view points on the use of properties within
classes. My belief is that properties should generally be used to return
values of private members. They should not do anything that is likely to
return an error and they should not instantiate or return any big objects
etc.

My company seems to be using them for everything which is starting to cause
me concern. Any comments or other viewpoints is appreciated.
1) a get should not change the object
2) a get should never throw an exception but may return null
3) a set may throw an exception
4) a get after a set should return the value just set
5) multiple set with the same value should have the same
effect as one set with that value

is what I would expect of a property.

Arne
Oct 26 '06 #26
Martijn Mulder wrote:
>Hi, i would like to get some view points on the use of properties

Properties are best avoided. They seem great at first, but in fact they
obscure your code. Suddenly there is a third kind of class member, data,
functions (methods) and now, NEW NEW NEW, properties. My experience is, when
I want to get some value, I use Get...(), when I want to set something, I
use Set...(value); Then you know where to look for what happens.
Of course you could do it with methods. C++ and Java does that.

But other C# programmers use and expect to see properties.

Whether or not you like properties as a concept does not
matter. To provide consistent code you should follow common
C# coding conventions. This includes properties.

I don't think properties is a benefit for the language, but
I do use them.

Arne
Oct 26 '06 #27
Martijn Mulder wrote:
Using properties is not a problem and indeed it looks rather neat. Writing
and maintaining your own properties does give some headache. Where, for
example, do you store them in the source file? I put fields at top, then the
constructor(s) followed by the methods, in alphabetical order. Properties
landed between the fields and the constructor, were hard to find, were hard
to maintain.
Switch from notepad to an IDE.

The have tools to help navigate.

Arne
Oct 26 '06 #28
"Mark Wilden" <mw*****@commun itymtm.comwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
Would the source compatibility part apply only to ref arguments?
No. If you want to later encapsulate the behavior of what was previously a
public field, you will require any user of the class to be modified and
recompiled.
Oct 26 '06 #29
Mark Wilden <mw*****@commun itymtm.comwrote :
On the other hand, it can cause problem if you *later* want any of the
above - you break both source and binary compatibility.

If binary compatibility is necessary, then you have to make up-front
decisions that would otherwise be better deferred. That's not the case for
most code, at least in my experience.

Would the source compatibility part apply only to ref arguments?
Yes, unless reflection is being used.

Of course, using fields means you can't use an interface to define the
contract of the class (or at least, you can't include the field in the
contract, whereas you can include a property).

--
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
Oct 26 '06 #30

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

Similar topics

2
3016
by: Rick Austin | last post by:
I recently had to perform a reinstalltion of Windows XP (my registry seems to have become corrupt). After this completed I had to reinstall all applications since most use the registry for settings, etc. After installing VS.NET 2003 everything seemed to work okay with one exception, none of the project properties appear. If I right click a project and select properties, the properties dialog appears and the property category tree is dispayed but...
4
7517
by: Lyn | last post by:
Hi, This question may seem a bit academic... To learn more about Access VBA, I have been enumerating the properties of various form controls. This was mostly successful and I have learned a lot from the process. It occurred to me that I could also enumerate the properties of the ADO Recordset in similar fashion, expecting to get back known properties such as AbsolutePosition, BOF, EOF, Filter, Sort, etc. I inserted the following
10
7375
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
6
5187
by: JerryP | last post by:
Hello, is there a way to launch the property dialogue for a directory from my c# app ? I would also like to launch the User Account Properties from Active Directory Users and Computers, and the properties window for an Object in Active Directory. Thanks for any hints.
3
5079
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank Martin
7
8537
by: Donald Grove | last post by:
Is it possible to retrieve field properties from a table in access2000 using code? I have tried: " dim dbs as dao.database dim tbl as dao.tabledef dim fld as dao.field dim prop as dao.property
1
1694
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled by their underlying classes (such as the TextBox control), and are not persisted by me. 2.) Unique Properties that don't map directly and are persisted in ViewState (ex.: this.LabelPosition, which specifies where on the form the label should be...
7
6072
by: Anderskj | last post by:
Hi! I am developing a c# application. I have a interface (which can change therefore my problem) If i do like this: List<PropertyInfoproperties = new List<PropertyInfo>(); properties.AddRange(typeof(app.IView).GetProperties());
0
2854
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to add/edit any values for a setting with type NameValueCollection. Nor can I get a NameValueCollection to persist to the User Settings via code using a simple C# Console App... Is this a user error or ?
4
9865
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
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
10595
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...
1
10341
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
10089
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
7634
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
6862
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3001
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.