Property: Encapsulation
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Property: Encapsulation
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ArrayList: Dynamic array in .NET [next Lesson]  Comparing Strings: For java programmers
Property: Encapsulation
  by: juliet

Property: Encapsulation in C#

by: juliet

Consider you have a class, call staff:
public class staff {
     public int Age;
     public string FirstName;
     public string LastName;
} 
When you need to reference to one of the attribute of the staff, you may like to reference it like this:
staff Bobby = new staff();
string myName = Bobby.FirstName + " " + Bobby.LastName;
This is what we consider as BAD programming practice in OOP concept, as the information about the staff class is not well encapsulated, but exposed easily to external classes.

In the C++ and Java days, we would do this:
public class staff {
       private int Age;
       private string FirstName;
       private string LastName;
       
       public string getFirstName() {
           return FirstName;
       }
       public void setFirstName(string strMyFirstName) {
           FirstName = strMyFirstName;
       }
       public string getLastName() {
           return LastName;
       }
       public void setLastName(string strMyLastName) {
           LastName = strMyLastName;
       }
   }
When we need to reference the name of staff in an external class, we would do this:
staff Bobby = new staff();
    string myName = Bobby.getFirstName() + " " + Bobby.getLastName();
This works perfectly over the last 20 years and this is how we encapsulate data in a class.

Now in C#, the concept of property arrives, which is taken from VB actually. This is the modern way of encapsulating data in a class now.
    public class staff {
       private int Age;
       private string FirstName;
       private string LastName;
       
       public string firstName {
         get {
             return FirstName;
         }
         set {
             FirstName = value;l
         }
         
         public string lastName { 
           get {
             return LastName;
           }
           set {
             LastName = value;
           }
         }
       }
   } 
When we reference the property, we do:
    staff Bobby = new staff();
    string myName = Bobby.firstName + " " + Bobby.lastName;
Clean, eh ?

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Property: Encapsulation
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ArrayList: Dynamic array in .NET [next Lesson]  Comparing Strings: For java programmers


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
ravikirank 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center