C#
  Home arrow C# arrow Page 3 - Behind the Scenes Look at C#: Properties c...
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 continued
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-07-20

    Table of Contents:
  • Behind the Scenes Look at C#: Properties continued
  • The Name Property
  • Static Properties
  • Read or Write Properties

  • 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 continued - Static Properties


    (Page 3 of 4 )

    Like static methods, static properties have defined on the class itself, not on an instance of the class. Sometimes you will find yourself in a situation where you have an attribute that has a class scope (a static field), which means that the field value is shared between all the instances of the class. For the same reasons that we have used properties with private fields, we will use a public static method with a private static field.

    You access the property using the class identifier (not in the form of instance.property). A static property accesses only a static field. This makes sense because the get/set methods need the this pointer to access the field. A static property doesn't have a pointer to this because it has a class scope. So an instance property can access a static field because static data are exposed to static and instance methods. The following updated version of the Worker example illustrates the use of static properties.

    using System;
    namespace property
    {
    class Class1
    {
    static void Main(string[] args)
    {
    try
    {
    Worker.Department = "IT";
    Worker w1 = new Worker();
    w1.FirstName = "Michael";
    w1.LastName = "Youssef";
    Console.WriteLine(w1);

    Console.WriteLine();

    Worker w2 = new Worker();
    w2.FirstName = "Maria";
    w2.LastName = "Meleka";
    Console.WriteLine(w2);
    Console.ReadLine();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.ReadLine();
    }
    }
    }

    public class Worker
    {
    private string firstName;
    private string lastName;
    private static string department;

    public string FirstName
    {
    get
    {
    return this.firstName;
    }

    set
    {
    if(value == String.Empty)
    {
    throw new ArgumentNullException();
    }
    else
    {
    this.firstName = value;
    }
    }
    }

    public string LastName
    {
    get
    {
    return this.lastName;
    }

    set
    {
    if(value == String.Empty)
    {
    throw new ArgumentNullException();
    }
    else
    {
    this.lastName = value;
    }
    }
    }

    public static string Department
    {
    get
    {
    return department;
    }

    set
    {
    if(value != "IT")
    {
    throw new ArgumentException("All Workers must be in the IT Department");
    }
    else
    {
    Worker.department = "IT";
    }
    }
    }

    public override string ToString()
    {
    return firstName +" "+lastName+" is working in "+department;
    }
    }
    }

    The result that you will get in the console window is this:

    We have declared the department private field as static, and the department property as static too. Note that by using a static property instead of a public static field we are able to do our data validation and throw exceptions, as we have done with the instance properties.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - 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#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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