Re: Property... why?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Gerry O'Brien [MVP] (gkcomput_at_hotmail.com)
Date: 11/23/04


Date: Tue, 23 Nov 2004 12:57:56 -0400

Perfect answer.

-- 
Gerry O'Brien
Visual Basic .NET MVP
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message 
news:%23%23aeXwX0EHA.2824@TK2MSFTNGP09.phx.gbl...
>>>if you want to access and modify your field, declare it as public is more
> efficient.
>
> Classes encapsulate data. When data changes the encapsulating class might
> need to know or inform some other class about it. Modifying a field does 
> not
> allow this mechanism to take place. It may be more efficient if you're
> seriously worried about the number of nanoseconds it takes to set a piece 
> of
> data these days but in general thats of little or no concern to anyone but
> the most pedantic programmer.
>
>>>But if you declared your field as private and want to change his value,
> !!!!
> there's a contradiction here because declaring your field as private means
> that you don't want to modify his value from outside the class.
>
> Declaring a field as private and accessing with a property is good object
> oriented technique. It enables a class to encapsulate data which it has 
> sole
> and complete control over. No external class can access and change that 
> data
> without the enacapsulating classes knowledge. This is a very important
> principle often overlooked by inexperienced programmers.
>
> -- 
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
> http://www.bobpowell.net/tipstricks.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/faqmain.htm
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>
>
>
>
>
> "MN" <1[n.m-recrut1]at2[ifrance2]dot3[com3]> wrote in message
> news:uVH3LRX0EHA.2316@TK2MSFTNGP15.phx.gbl...
>> Hi,
>>
>> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> a écrit dans le 
>> message
>> de news: uZbjs3W0EHA.3364@TK2MSFTNGP12.phx.gbl...
>> > One big answer to Why? is because the design time environment relies on
>> > properties and the use of attributes applied to properties.
>> > Public fields aren't seen in the designer so if you want the object to
>> > interact at design
>> > time you need to use properties.
>>
>> It's a BIG error that public fields aren't seen in the designer.
>>
>> The answer to why using property depend on why using private or public
>> field?
>> if you want to access and modify your field, declare it as public is more
>> efficient.
>> Now if you want only to access your field, declare it as private and
> create
>> a get method (or property) that return the value;
>> But if you declared your field as private and want to change his value,
> !!!!
>> there's a contradiction here because declaring your field as private 
>> means
>> that you don't want to modify his value from outside the class.
>>
>>
>>
>
> 


Relevant Pages

  • Re: Property... why?
    ... is because the design time environment relies on ... It's a BIG error that public fields aren't seen in the designer. ... Now if you want only to access your field, declare it as private and create ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Property... why?
    ... >>if you want to access and modify your field, declare it as public is more ... that you don't want to modify his value from outside the class. ... Declaring a field as private and accessing with a property is good object ... It enables a class to encapsulate data which it has sole ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Declaring subs Public vs Private
    ... IMO you should always declare, explicitly stating whether you want it to be ... What should be declared Private vs not declared at all, just sub ... For example if you wnat to modify a sub ...
    (microsoft.public.excel.programming)
  • Re: const int value changing....
    ... >>> If you want to modify an object, don't declare it const. ... >>> declare an object const, ... Solid meets fluid: A constant should not be changed. ...
    (comp.lang.c)
  • Re: Local copy of variable not acting locally
    ... > I want to declare a local copy of an object so I can modify the local ... > version without affecting the original copy. ... > Private Sub MySub1 ... my intention is to modify MyLocalObject without ...
    (microsoft.public.dotnet.languages.vb)