473,796 Members | 2,445 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 #1
39 1952
I think that really depends on the object in question, I don't know if it's
fair to make absolute rules.

Yes, generally properties just return values of private members. But if
that is all they ever did, there really wouldn't be much of a point to them,
would there? There are instances where a property must do some validation,
or some other processing in order to keep the object in a consistent state
given that the value of this property is being changed.

Sometimes a property needs to point to another big object, just because the
two objects are linked together and they need to talk to each other.

It's really hard to say one way or the other for a vague non specific
situation. I'm sure any construct can be misused, but in the end it all
really depends on the application and the objects involved as to what is
appropriate. At least that's my 2 cents.

"Paul Mcilreavy" <fa********@dis cussions.micros oft.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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 #2
"Paul Mcilreavy" <fa********@dis cussions.micros oft.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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.
Well, this is bound to vary somewhat from person to person, since there are
no hard and fast rules. But my opinion as to the proper use of properties
is generally this:

* Properties are used when they can return something that is
semantically a characteristic of the object, whether static (doesn't change
during the lifetime of the object) or dynamic (may in fact change during the
lifetime of the object). If you can think of a value as somehow being tied
to the object itself, then it can be implemented as a property.

* Methods are used when one is specifically asking an object to *do*
something. This may involve returning some value, just as a property does,
or it may not. The key is that the method is used when the important thing
is the *doing*, as opposed to the *being* that a property describes.

To me, properties are nouns and adjectives related to the class, while
methods are the verbs of the class.

Note that in neither of those descriptions is there any mention of how much
effort it takes to accomplish one or the other. I would agree that
generally speaking, a property should be relatively lightweight, especially
with respect to execution time. But I don't feel this is a hard-and-fast
rule (and besides, it could also be argued that most methods should also be
relatively lightweight, especially with respect to execution time).

If semantically it makes sense for something that takes a fair amount of
work to retrieve before returning it to the caller or before changing the
state of an object (remember that properties can be retrieved, set, or both)
to still be considered a property, I think the semantics should overrule the
performance issues. After all, if you have some amount of work to be done,
it's the same whether you call it a method or a property. Imposing
artificial, implementation-related restrictions on what is essentially a
semantic question is wrong, IMHO.

As far as the question of the size of an object being returned, IMHO that's
a complete non-issue. If semantically it makes sense for a very large
object to be returned by a property, then so be it. Return it from a
property. The size of the data has nothing to do with whether something
should be a property or a method. Some methods may return a single integer
value, while some properties may return an array with tens of thousands of
elements.
My company seems to be using them for everything which is starting to
cause me concern. Any comments or other viewpoints is appreciated.
You should be concerned if properties are being used for literally
*everything*. But not for the reasons you suggest.

Pete
Oct 24 '06 #3
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.


Oct 24 '06 #4
yep, they would need to do some validation etc on occaision. i guess i meant
that if a property is over 10-20 lines...doing alot of validation or dealing
with big obects that can generate errors...then it should probably be a
method rather than a property
"Marina Levit [MVP]" <so*****@nospam .comwrote in message
news:ug******** *****@TK2MSFTNG P04.phx.gbl...
>I think that really depends on the object in question, I don't know if it's
fair to make absolute rules.

Yes, generally properties just return values of private members. But if
that is all they ever did, there really wouldn't be much of a point to
them, would there? There are instances where a property must do some
validation, or some other processing in order to keep the object in a
consistent state given that the value of this property is being changed.

Sometimes a property needs to point to another big object, just because
the two objects are linked together and they need to talk to each other.

It's really hard to say one way or the other for a vague non specific
situation. I'm sure any construct can be misused, but in the end it all
really depends on the application and the objects involved as to what is
appropriate. At least that's my 2 cents.

"Paul Mcilreavy" <fa********@dis cussions.micros oft.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>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 #5
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.
While it may be a religious issue, I for one strongly agree with you. It
seems likely that the language's authors even intended it this way. They
should usually provide fast, efficient and reliable access. It's cleaner and
really reflects their intended usage. Moreover, I think most people usually
assume this so if a property isn't efficient and people are frequently
calling it (perhaps in tight loops), then it can potentially have a negative
impact on your app's performance. Finally, as a purely practical issue, see
the following two links (by MSFT employees). I've experienced frustrating
problems with these issues and was personally told by a member of the VS
debugging team (Azeem Khan) that simple properties (returning member
variables only) can significantly help to avoid these problems.

http://blogs.msdn.com/greggm/archive...18/494648.aspx
http://blogs.msdn.com/greggm/archive.../04/67766.aspx
Oct 24 '06 #6
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.
Oct 24 '06 #7


I use "derived" properties at time.

Think about the age of a person. It is based on today's date , and hte dob
(date of birth ) of a person.

So ... I'll expose a "derived" property, usually readonly.

public int AgeInYears
{
get
{
return this.Deterimine AgeInYears();
}
}

private int DeterimineAgeIn Years
{
if (this.m_dob != DateTime.MinDat e )
{
return DateTime.Now.Su bstract( this.m_dob ).Years;
}
else
{
// throw new Exception("DOB was not supplied"); //<- throw an exception?
I usually don't. But its an option.
return 0;
}
}
Something along those lines.
Should I just expose the private method DeterimineAgeIn Years ? Maybe? It
just depends I guess.
Sometimes I have the ".Status" property where I need to check 3 or 4 things.
But there's some food for thought. When its a "derived" property, then I do
more than just return a member variable.
At the same time, 99.8% of the time, returning a member variable is all I
do.


"Paul Mcilreavy" <fa********@dis cussions.micros oft.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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 #8
"Martijn Mulder" <i@mwrote in message
news:45******** *************** @news.wanadoo.n l...
>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.
If you don't like properties, perhaps you should program in another
language.
Properties are an integral part of C#.

Bill
Oct 24 '06 #9
>>Hi, i would like to get some view points on the use of properties
>Properties are best avoided. They seem great at first, but...
It seams that you totally missed the entire concept of object orientation.
Totally... entire. Wow. Properties are just another way of implementing
Set...(value) and Get...() methods. In fact, the compiler substitutes
properties with methods with that signature. Totally and entire
Oct 25 '06 #10

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

Similar topics

2
3015
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
7372
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
5185
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
8534
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
1692
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
6070
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
2853
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
9864
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
9679
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
10453
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...
0
10223
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
10172
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
10003
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
7546
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
6785
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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

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.