.NET Tip: Data Encapsulation: Using Properties Instead of Public Variables Agoura Hills CA

Here is encapsulation, one of the very basic tenets of object-oriented programming. Using properties instead of public variables helps insulate the users of your class from change, provides you more flexibility, and will ease maintenance of your class.

Local Companies

U-PIC Insurance Services
800-955-4623
28001 Dorothy Dr
Agoura Hills, CA
BPI
(818) 347-7122
24500 Indian Hill
West Hills, CA
Astoundit Software, LLC
805-492-2506
1534 N. Moorpark Rd
Thousand Oaks, CA
Murano Software, Inc.
(818) 340-5032
21323 Dumetz Road
Los Angeles, CA
FocusBiz Software
818-635-6335
CA
Callbox, Inc.
(310) 439-5814
16770 Encino Hills Dr. Suite 200
Encino, CA
Pixel by Pixel
310.487.6912
1742 S Bentley Ave
Los Angeles, CA
I(2) Drive
310 498-2955
2344 28th Street
Santa Monica, CA
Valortek
866-640-8181
Beverly Hills, CA
TimeTECH - Customizable Time and Attendance / Workforce Management Solutions
905-677-7009
7420 Airport Rd 203
Mississauga, CA

.NET Tip: Data Encapsulation: Using Properties Instead of Public Variables

provided by: 
Originally published at Internet.com


I have run across some old code recently where some classes exposed public variables directly to the outside world. Even though the code works, it is very fragile. The code looked something like this: public class Person { public string Name; public int Age; }

A much better practice is to make class variables private and expose them through public properties, thus encapsulating the details of your implementation. This brings several benefits, including that ability to override the property in derived classes or to easily add validation logic when the property value is set. You also can create calculated properties whose values are determined only when they are accessed. A version of the Person class using properties might look like this: public class Person { private string _Name; private int _Age; public string Name { get { return _Name; } set { _Name = value; } } public int Age { get { return _Age; } set { _Age = value; } } }

As you can see, using properties does take several more lines of code than exposing public variables. The cost, though, is well worth the benefit to your application. If you are using Visual Studio 2008, you can use the new support for the automatic properties to gain the benefit of properties with syntax that is very similar to using public variables. Here is the Person class in Visual Studio 2008 using automatic properties: public class Person { public string Name { get; set; } public int Age { get; set; } }

In the long run, I think you will find that using properties makes your classes more stable and easier to extend.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

U-PIC Insurance Services

800-955-4623
28001 Dorothy Dr
Agoura Hills, CA
http://www.u-pic.com

Related Local Events
Windows Hardware Engineering Conference
Dates: 11/5/2008 - 11/7/2008
Location: Los Angeles Convention Center
Los Angeles CA
View Details

Digital Video Expo
Dates: 11/4/2008 - 11/6/2008
Location: Los Angeles Convention Center
Los Angeles CA
View Details

IT ChannelVision Fall 2008
Dates: 10/4/2008 - 10/8/2008
Location: Hyatt Regency Los Angeles
Los Angeles CA
View Details

IT ChannelVision Fall 2008
Dates: 10/4/2008 - 10/8/2008
Location: Hyatt Regency Los Angeles
Los Angeles CA
View Details

Biotechnology Vendor Showcase - University of California, Los Angeles
Dates: 10/2/2008 - 10/2/2008
Location: UCLA
Los Angeles CA
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Retail & Consumer Services
Business Services Entertainment Industrial Goods & Services Software
Career Family Insurance Technology
Cars Financial Services Internet Telecommunications
Computer Hardware Food & Beverage Legal Transportation & Logistics
Construction Health Pets Travel
Education Home Electronics Real Estate Wedding