Re: Why use properties?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: James Curran (JamesCurran_at_mvps.org)
Date: 12/03/04


Date: Fri, 3 Dec 2004 12:13:10 -0500

Funny thing is --- I was just about to post a very similar question to the
group.

However, I'm still a bit more skeptical. Of all the answers given, only
Nicholas's held water.

All the rest boil down to, "You need a property -- if you need more than
simple assignment."

But the question was, "what's the advantage of a property if all I need (for
now) is simple assignment?".

Saying
 private int salesTotal;
 public int SalesTotal
 {
     get {return salesTotal;}
     set {salesTotal=value;}
 }

is better than

 public int SalesTotal;

because I might want to change the get/set at some time in the future is a
false lead, as I could convert the latter into the former at any time.
Basically, it comes down to doing more typing now, to potentially save the
same amount of typing later. By the same principle which leads us to
lazy-loading information from databases, would lead us to prefer the public
variable until such time as we need to convert it to a Property.

-- 
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com       Work: www.njtheater.com
Blog: www.honestillusion.com  Day Job: www.partsearch.com
"Janaka" <janakaf@hotmail.com> wrote in message
news:ewyfUGV2EHA.3596@TK2MSFTNGP12.phx.gbl...
> Thanks for all the replies.
>
> I believe the main reason why to code in a skeleton property with just a
> get/set is to provide future-proofing and easier maintenance in the long
> run.
>
> Tom your reply was even approved by our head sceptic.
> public int SalesTotal;
> >
> > rather than
> >
> > private int salesTotal;
> > public int SalesTotal
> > {
> > get {return salesTotal;}
> > set {salesTotal=value;}
> > }
> >
>
>


Relevant Pages

  • Re: Explicit variable declaration
    ... typing can be appreciated by some people. ... type declarations can't be enforced without losing a lot of the power ... the names listed in it could be assigned in the local namespace, and assignment to other names wasn't allowed. ...
    (comp.lang.python)
  • Re: how to wrap your brain around delegates?
    ... accessors rather than get/set. ... stuff you shouldn't do, like assignment. ... Delphi/InterBase Weblog: http://blogs.teamb.com/craigstuntz ...
    (borland.public.delphi.non-technical)
  • Re: why still use C?
    ... It can now detect assignment to the wrong type of ... > And to combat your idot proofing the world makes a better idiot... ... > ...but I fail to see how going from typing something once to typing it ...
    (comp.lang.c)
  • Re: How to "or" a generic array of std_logic_vector ?
    ... to use a generate statement...basically the same amount of typing and debug ... The problem (latches) with this solution as well as that in the OP is ... that there is no assignment to data_out when the row is 0. ...
    (comp.lang.vhdl)
  • Re: new version of visual basic is going to be released
    ... you are entitled to choose another language. ... >> for stuff that is obvious and shouldn't require a lot of typing. ... Most modern compilers will warn you if you are doing an assignment in a ...
    (microsoft.public.vb.general.discussion)