PC Review


Reply
Thread Tools Rate Thread

Design issue: Property vs. Method

 
 
Jeff Robichaud
Guest
Posts: n/a
 
      24th Feb 2005
Hello,

I have an obect that needs to inform the outside world wheter prices are
synchronized or not. My first idea was to use a public property for this:

public bool PricesSynchronized

But I need to access a database and the query takes a (relatively) long time
to run. So for any code that will use this property, I just find it strange
that it's going to take time, because usually a public property is used to
simply publish some internal data, sometimes performing simple calculations,
sometimes just preveting write acces to internal data (using get without
set).

I have never seen a property that takes time to process so I would tend to
use a method here instead. But on the other hand, methods are usually used
to perform tasks on the object, whereas properties are used to get some info
about the object...I know this is somehow philosophical but what would be
the best choice here (from a design point of view) ??....


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      24th Feb 2005

>I know this is somehow philosophical but what would be
>the best choice here (from a design point of view) ??....


I'd use a method in this situation. I believe the issue was discussed
in
http://msdn.microsoft.com/netframewo...s/membertypes/



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Carlos J. Quintero [.NET MVP]
Guest
Posts: n/a
 
      25th Feb 2005
I think to recall that MS recommendation (and common sense) is to use
methods for actions that can take some time to be executed. Properties are
expected to be executed very fast.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"Jeff Robichaud" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> Hello,
>
> I have an obect that needs to inform the outside world wheter prices are
> synchronized or not. My first idea was to use a public property for this:
>
> public bool PricesSynchronized
>
> But I need to access a database and the query takes a (relatively) long
> time to run. So for any code that will use this property, I just find it
> strange that it's going to take time, because usually a public property is
> used to simply publish some internal data, sometimes performing simple
> calculations, sometimes just preveting write acces to internal data (using
> get without set).
>
> I have never seen a property that takes time to process so I would tend to
> use a method here instead. But on the other hand, methods are usually used
> to perform tasks on the object, whereas properties are used to get some
> info about the object...I know this is somehow philosophical but what
> would be the best choice here (from a design point of view) ??....
>
>



 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      25th Feb 2005
I would have a method called ".SyncPrices()" on the object, and then a
property that returns the specific data. That way, you are seperating the
calculation of the information from its use.

In general, it is poor practice to put code into a property that will take
substantial time to execute or could raise errors that the caller would be
surprised to see. In other words, how strange would it be to get a SQL
error on the following line?

bool ArePricesSynced = myObject.PricesSynchronized;


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Jeff Robichaud" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I have an obect that needs to inform the outside world wheter prices are
> synchronized or not. My first idea was to use a public property for this:
>
> public bool PricesSynchronized
>
> But I need to access a database and the query takes a (relatively) long
> time to run. So for any code that will use this property, I just find it
> strange that it's going to take time, because usually a public property is
> used to simply publish some internal data, sometimes performing simple
> calculations, sometimes just preveting write acces to internal data (using
> get without set).
>
> I have never seen a property that takes time to process so I would tend to
> use a method here instead. But on the other hand, methods are usually used
> to perform tasks on the object, whereas properties are used to get some
> info about the object...I know this is somehow philosophical but what
> would be the best choice here (from a design point of view) ??....
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design issue about which class a method should belong to Tony Johansson Microsoft C# .NET 3 17th Jan 2010 11:44 PM
Issue when accessing the property/method of a Calender control =?Utf-8?B?QW5hbmQ=?= Microsoft Excel Programming 0 12th Jan 2007 06:36 AM
property with defalut value will not be called in auto-generated code, and how to make a property only visible at design time? Ryan Liu Microsoft C# .NET 2 27th Aug 2006 04:09 PM
can a custom control have an image property that can be set in design time from the property browser? news.austin.rr.com Microsoft Dot NET Compact Framework 3 3rd Mar 2005 03:08 AM
Re: How can I disable/enable a design Property depending on antoher Property NULL Microsoft C# .NET 2 4th Sep 2003 05:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:40 PM.