Re: Property... why?
From: Gerry O'Brien [MVP] (gkcomput_at_hotmail.com)
Date: 11/23/04
- Next message: Dot Net Newbie: "Re: Send Strongly-typed Dataset with Multiple Tables to SQL Server"
- Previous message: dcstech: "Re: Program Loading"
- In reply to: Bob Powell [MVP]: "Re: Property... why?"
- Next in thread: Cor Ligthert: "Re: Property... why?"
- Reply: Cor Ligthert: "Re: Property... why?"
- Messages sorted by: [ date ] [ thread ]
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. >> >> >> > >
- Next message: Dot Net Newbie: "Re: Send Strongly-typed Dataset with Multiple Tables to SQL Server"
- Previous message: dcstech: "Re: Program Loading"
- In reply to: Bob Powell [MVP]: "Re: Property... why?"
- Next in thread: Cor Ligthert: "Re: Property... why?"
- Reply: Cor Ligthert: "Re: Property... why?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|