Adding Methods to Classes


Adding Methods to Classes

Methods define the behavior of the class. There are two types of methods (also referred to as functions ) in C#: instance methods and static methods.

Instance methods apply their behavior to a specific instance of the class. For example, imagine an instance method called Make-Deposit, a member of the Account class. If you create two account objects, you can invoke the MakeDeposit through either object; calling MakeDeposit through the first object only affects the balance of the first, while calling MakeDeposit through the second object only affects the balance of the second.

Static methods are global methods that don't apply their behavior to any particular instance of the class. (You will learn about static methods in Chapter 6, "Special Members.")

To add an instance method:

  1. Inside a class definition, begin a new line.

  2. Add a function to the class as you learned earlier in the chapter. Remember that functions can return a parameter of any type; or if you don't wish to return a value, type the word void ( Figure 2.75 ).

    Figure 2.75 Enhance the classes for the sample project by adding functions.
     class ToDoItem {    string item = "";    string description = "";  public void SetInfo(string item,   string description)  {      this.item = item;      this.description = description;    } } class ToDoList {      Hashtable list = new Hashtable();      //adds the ToDoItem to the      //hash table  public void Add(ToDoItem tditem)  {         list.Add(tditem.Item,tditem.Description);      }      //removes the ToDoItem from      //the hash table  public void Remove(ToDoItem tditem)  {         list.Remove(tditem.Item);      } } 

graphics/tick.gif Tips

  • All methods inside a class are private by default. This means they can only be used by code inside of the class. Until you learn about scope modifiers you can declare the methods as public if you wish to use them outside of the class ( Figure 2.76 ). A full explanation of scope modifiers requires an explanation of inheritance, found in Chapter 5, "Class Inheritance."

    Figure 2.76 Functions, like fields and properties are private; they can only be invoked from other functions in the same class. To use it outside of the class you can mark the function public.
     class Cat {    public void Call()    {    }    //function is private    void Respond()    {    } } class Owner {    void LookForKitty()    {      Cat tiger = new Cat();      tiger.Call(); //legal, Call is                    //public  tiger.Respond(); //***illegal,   //Respond   //is private  } } 
  • You can't have a field with the same name as a function ( Figure 2.77 ), but you can have two methods with the same name as long as you change either the number of parameters in the declaration, or the type of one of the parameters ( Figure 2.78 ). This mechanism is called method overloading and it is discussed in Chapter 6, "Special Members."

    Figure 2.77 You can't add a field and a method with the same name to the same class. But because C# is case sensitive you can essentially have the same name if you change the casing of one of the characters in one of the names .
     class Toothbrush {    //this is illegal, you can't have    //both a field and a method (or a    //property for that matter) named    //the same thing    string Owner;    void Owner(string name)    {    } } 
    Figure 2.78 Method overloading is the idea that you can add multiple versions of the same method to the same class. To do method overloading at least one of the parameters in the declaration has to be different from the other declarations (either a different type or a different number of parameters.)
     class Checking {    public void  MakeDeposit  (decimal amount)    {    }    public void  MakeDeposit  (int amount)    {    }    public void  MakeDeposit  (decimal amount,                           decimal available)    {    } } 



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