473,804 Members | 2,154 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...
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.
Okay, now I'm a little lost I think. I must be reading "source
compatibility" differently than intended, since I got the exact opposite
answer to the question than you did. :)

My interpretation:

"binary compatibility" -- you don't need to recompile
"source compatibility" -- you don't need to change the source code

In other words, if you have a public field, and decide to make that private
to support some desired behavior that using a property gives you, you have
to go modify the code. That breaks what I know as "source compatibility".

So, clue me in...what ARE you guys talking about, and why does it only
affect "ref arguments"? (which I read to mean "arguments using the 'ref'
keyword"...that is, arguments passed by reference).

Thanks,
Pete
Oct 26 '06 #31

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.coms chrieb im Newsbeitrag
news:12******** *****@corp.supe rnews.com...
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>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.

Okay, now I'm a little lost I think. I must be reading "source
compatibility" differently than intended, since I got the exact opposite
answer to the question than you did. :)

My interpretation:

"binary compatibility" -- you don't need to recompile
"source compatibility" -- you don't need to change the source code

In other words, if you have a public field, and decide to make that
private to support some desired behavior that using a property gives you,
you have to go modify the code. That breaks what I know as "source
compatibility".
This surely would break source compatibility as well as binary comptability.
What they meant is replacing the public Field with property of the same
name, with a field with another name. Since access to fields an properties
are same in C# but different in IL that wouldn't break source compatibility
but binary compatability.
>
So, clue me in...what ARE you guys talking about, and why does it only
affect "ref arguments"? (which I read to mean "arguments using the 'ref'
keyword"...that is, arguments passed by reference).
Fields can by passed as ref/out parameters while properties can't.
>
Thanks,
Pete

Oct 26 '06 #32
It would also trash any reflective usage that explicetely looks for a
field... so you'd need to look for the field/property name as a string.

Not that I condone reflection as the first answer to a problem, but it has a
few uses...

Marc
Oct 26 '06 #33
>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.
:-) no, I stick with vim. Working with properties became cumbersome when I
had to decide how to address fields within the class itself. Private,
protected or public is not an issue there, but some of the validation was
stored in the property-accessor. So I had two different outlooks on the same
internal data. My methods, that are usually very short and concise, became
difficult to interpret because raw data access was mingled with
property-based access. One day I decided to kick all home-grown properties
out and that cleared things up a lot (although they seem to be creeping
back)
Oct 26 '06 #34
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:12******** *****@corp.supe rnews.com...
>
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.
I'm talking about converting public fields to public getter/setters. I don't
see how client source would need to be modified (unless the fields were
passed as ref arguments). The client would need to be recompiled (which is
important if and only if binary compatibility is required).

///ark
Oct 26 '06 #35
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>
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).
Again, this is a no-brainer. -IF- you need to do this, then of course you
will use properties. My comment is whether one should just automatically use
getter/setters to wrap fields instead of using public properties when it
is -possible- to do so.

And, again, I feel that getter/setter property pairs are in general a bad
idea anyway.

///ark
Oct 26 '06 #36
Mark Wilden <mw*****@commun itymtm.comwrote :
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).

Again, this is a no-brainer. -IF- you need to do this, then of course you
will use properties. My comment is whether one should just automatically use
getter/setters to wrap fields instead of using public properties when it
is -possible- to do so.

And, again, I feel that getter/setter property pairs are in general a bad
idea anyway.
When are they a bad idea but public fields are a *good* idea?

--
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 #37
"Mark Wilden" <mw*****@commun itymtm.comwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
>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.

I'm talking about converting public fields to public getter/setters. I
don't see how client source would need to be modified (unless the fields
were passed as ref arguments).
Ahh...I guess for some reason I just assumed that in the process, the public
field would be renamed. On reflection, I see that was a dumb assumption.
:)

Not sure why I made that assumption...I mean, I use fairly strict naming
conventions in which the names of fields and the names of properties would
never be the same, but then I would never make a public field anyway, so my
own personal preferences obviously have nothing to do with the way someone
else might do it. :)

Thanks...

Pete
Oct 26 '06 #38
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>And, again, I feel that getter/setter property pairs are in general a bad
idea anyway.

When are they a bad idea but public fields are a *good* idea?
What I was saying is that they are both code smells. "Encapsulat ing" full
access to one's innards simply by wrapping those innards in properties is
usually just a band-aid.
Oct 26 '06 #39

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

Okay, now I'm a little lost I think. I must be reading "source
compatibility" differently than intended, since I got the exact opposite
answer to the question than you did. :)

My interpretation:

"binary compatibility" -- you don't need to recompile
"source compatibility" -- you don't need to change the source code
I think that you need to narrow what you mean by "source code".

"binary compatibility" -- you don't need to recompile _client_
applications for changes in the library code.
"source compatibility" -- you don't need to change the code of _client_
applications for changes in the library code.

In all cases, of course, you have to change the source code for the
class in question, and recompile.

So if you make a public field private and make a public property for
it, you of course have to recompile the code for that class.
Compatibility just asks whether you have to recompile / modify any of
the code _using_ the changed class.

Oct 27 '06 #40

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
9712
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
10343
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
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
5530
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.