Building Code Libraries with Static Members


Building Code Libraries with Static Members

All the functions and fields discussed so far in the code examples have been instance members. These are members that can only be invoked if you create an instance of the class. In other words, before you call MakeDeposit, for example, you must create an Account object.

Sometimes a function or a field doesn't need to be attached to a certain object. In the case of the Account class one can envision each object storing a Balance and each time you call MakeDeposit, the MakeDeposit function must execute in the context of the object through which the function was executed. In other words, if you write acct1.MakeDeposit(500) then you are increasing the balance for the object stored in acct1.

But suppose that you want to create a Math librarya class that contains a number of math functions. The functions must be part of a class because in C# all code must be part of a class. The Math class may have an Add function and a Subtract function. The Add function takes two numbers, adds them and returns the result; the Subtract function takes two numbers , subtracts the second from the first and returns the result. In this scenario it would be awkward to have to create objects of type Math just to invoke these functions.

So you might use a static function instead. Static functions are functions that can be invoked without creating an instance of the class. Along the same lines, static fields are global fields that store a single value for all the objects of the same type. Static functions are limited in that they can only invoke other static functions and only use static fields.

To add and call static members:

  1. Type static in front of a field to make it a type field.

    or

    Type static in front of a function to make it a type function.

  2. Make sure the code for the static function doesn't reference non-static fields or non-static functions ( Figure 6.14 ).

    Figure 6.14 You can't have global variables in C#, but you can simulate them with static variables. Static variables are more manageable than old global variables because they are attached to a particular type.
     class Account {  static  public int TotalAccounts;  static  public int    GetNextAccountNumber()    {        TotalAccounts++;      return TotalAccounts;    } } 
  3. To call a static function in code, use the class name plus a period plus the name of the function. If the code is inside the class where the static functions are declared, you don't have to use the class name ( Figure 6.15 ).

    Figure 6.15 To call a static function you don't create an instance of the class that contains the static function, you simply use the name of the class plus the name of the function.
     class Bank {    void CreateAccount()    {      //add this code to the code      //in Figure 6-14      int iTotal =  Account.GetNextAccountNumber;  Response.Write(      "Total number of Accounts=" +      iTotal.ToString());    } } 

graphics/tick.gif Tips

  • Use static fields when you need to store information that relates to all of the instances of the class and not just to a single instance. In Figure 6.14 this is done to store the total number of account objects that have been created.

  • The value stored in a static field lives for the duration of the program.

  • Use static functions when building code libraries ( Figure 6.16 ). Code libraries are groups of functions that can be invoked without creating instances of the class.

    Figure 6.16 One reason people use static functions is to create a library of global functions. Notice that the Math class serves as a library of Math- related functions.
     class Math {  static  public int Add(int x1, int x2)    {      return x1+x2;    }  static  public int Subtract                       (int x1, int x2)    {      return x1-x2;    } } class Homework {    void DoMath()    {      int iTotal =  Math.Add(5,7);  Response.Write("5 + 7 = " +      iTotal.ToString());    } } 
  • You can add a static constructor to your class. The constructor must be marked public and must not have any parameters. A static constructor is also referred to as a type initializer. The static constructor will trigger only once during program execution, and normally triggers the first time any of the code refers to the class.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net