C#
  Home arrow C# arrow Behind the Scenes Look at C#: Properties
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Behind the Scenes Look at C#: Properties
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-07-13

    Table of Contents:
  • Behind the Scenes Look at C#: Properties
  • The Private Fields Example
  • C# Properties
  • Modifying the Worker Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Behind the Scenes Look at C#: Properties


    (Page 1 of 4 )

    Without properties you define your fields as public (not private). By doing that, you give client code access to the fields and allow it to modify the values as much as they want. In this article we will discuss coding techniques without properties, with properties, and more.

    The Public Fields Example

    In the previous articles we have used public fields to store values, but this is not a good Object Oriented Programming practice. Let's see why. Compile the following code and run it:

    using System;
    namespace Properties
    {
    class Class1
    {
    static void Main(string[] args)
    {
    // this code is fine
    Worker worker1 = new Worker();
    worker1.FirstName = "Michael";
    worker1.LastName = "Youssef";
    worker1.Department = "IT";
    worker1.Age = 22;

    // this code is not fine
    Worker worker2 = new Worker();
    worker2.FirstName = "Steve";
    worker2.LastName = "John";
    worker2.Department = "NONE";
    worker2.Age = 900;

    Console.WriteLine(worker1);
    Console.WriteLine(worker2);
    Console.ReadLine();
    }
    }

    public class Worker
    {
    public string FirstName;
    public string LastName;
    public string Department;
    public int Age;

    public override string ToString()
    {
    return FirstName +" "+LastName+" is working in "+Department+
    " and he's "+Age+" years old";
    }

    }
    }

    You will get the following result to the console. Do you like it?

    Steven John is working in NONE and he's 900 years old. As you can tell, the object worker2 has invalid data and its state is not maintained. The client code (our Main method) has accessed the public fields of the object and assigned values to the fields which yield us this mess.

    We need a way to secure our data, so the first thing that comes to our mind is to declare these fields as private and use methods to set and get the values. Using this technique, we can write code to check the user input (suppose that the value 900 has been input from the user) and accept only the data that is valid to the object and to our business rules. For example, you have a business rule that states that the worker age must be less than 40 years old on the day of hiring, and you have another rule that states that the worker nationality is American because you don't hire international workers, and so on.

    The method that returns or gets the value of the private field is called a get method. By convention programmers used to call it a get accessor because this method has access to a private field, and it's the only way to get this private field's value. The method that sets or assigns a value to private fields is called a set method. Programmers used to call it a set accessor because it has code that sets a value to the private field, and maybe after some validation code, checks that the data is valid for this field.

    By convention, programmers used to name the get methods with the prefix "Get." If you have a private field called firstName, it's better to call the get method GetFirstName(). The same holds true with the set methods, so the set method for the firstName field is SetFirstName() and so on. 

    Let's modify the above example to secure the object's fields.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT