C#
  Home arrow C# arrow Page 3 - Handling Methods and Functions
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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#

Handling Methods and Functions
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-11-03

    Table of Contents:
  • Handling Methods and Functions
  • Overloading Methods and Constructors
  • Encapsulating Data with Properties
  • The get Accessor
  • Property Access Modifiers

  • 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


    Handling Methods and Functions - Encapsulating Data with Properties


    (Page 3 of 5 )

    Properties allow clients to access class state as though they were accessing member fields directly, while actually implementing that access through a class method.

    This is ideal. The client wants direct access to the state of the object and doesn’t want to work with methods. The class designer, however, wants to hide the internal state of his class in class members, and provide indirect access through a method.

    By decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed. When theTimeclass is first created, theHourvalue might be stored as a member variable. When the class is redesigned, theHour value might be computed or retrieved from a database. If the client had direct access to the originalHourmember variable, the change to computing the value would break the client. By decoupling and forcing the client to go through a method (or property), theTimeclass can change how it manages its internal state without breaking client code.

    Properties meet both goals: they provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data-hiding required by good object-oriented design, as illustrated in Example 4-11.

    Example 4-11. Using a property

    #region Using directives

    using System;
    using System.Collections.Generic;
    using System.Text;

    #endregion

    namespace UsingAProperty
    {
     
    public class Time
      {
       
    // private member variables
       
    private int year;
        private int month;
        private int date;
        private int hour;
        private int minute;
        private int second;

        // public accessor methods
        public void DisplayCurrentTime()
        {

          System.Console.WriteLine(
            "Time\t: {0}/{1}/{2} {3}:{4}:{5}",
            month, date, year, hour, minute, second );
       
    }

        // constructors
        public Time( System.DateTime dt )
        {
         
    year = dt.Year;
          month = dt.Month;
          date = dt.Day;
          hour = dt.Hour;
          minute = dt.Minute;
          second = dt.Second;
        }

        // create a property

        public int Hour
       
    {
          get
          {
           
    return hour;
          }

          set
          {
            hour = value;
          }
        }
      }

      public class Tester
     
    {
        static void Main()
        {
          System.DateTime currentTime = System.DateTime.Now;
          Time t = new Time( currentTime );
          t.DisplayCurrentTime();

          int theHour = t.Hour;
          System.Console.WriteLine( "\nRetrieved the hour: {0}\n",
            theHour );
         
    theHour++;
         
    t.Hour = theHour;
         
    System.Console.WriteLine( "Updated the hour: {0}\n", theHour );
       
    }
      }
    }

    To declare a property, write the property type and name followed by a pair of braces. Within the braces you may declaregetandsetaccessors. Neither of these has explicit parameters, though theset(), accessor has an implicit parametervalue, as shown next.

    In Example 4-11,Houris a property. Its declaration creates two accessors:getandset:

      public int Hour
      {
        get
        {
          return hour;
        }

        set
        {
          hour = value;
        }
      }

    Each accessor has an accessor body that does the work of retrieving and setting the property value. The property value might be stored in a database (in which case the accessor body would do whatever work is needed to interact with the database), or it might just be stored in a private member variable:

      private int hour;

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - C# Meets Design Patterns
    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek