2.3 Constructing Class Methods

 <  Day Day Up  >  

2.3 Constructing Class Methods

You want to add a new method to your class.


Technique

Select the class within the Class View window and click on Project, Add Method (Alt+P, M). Specify the correct access modifier, return type, and name of the method in the dialog. For each parameter in the method, set any applicable parameter modifiers, such as the type and name of the parameter. Click on the Add button to add the parameter to the parameter list. Finally, click the check box for any method modifier you need.

Comments

Adding a new method to a class can be quite involved given the sheer number of options you have available to you. The required elements of a method include the return type, the name of the element, and a parameter list, which can be empty if your method doesn't require them. All of the other fields in the Add Method dialog are optional, but it is important to understand how they affect the behavior of your method.

Looking at the available method modifiers, you can see the same static keyword that you used for fields earlier. Although the effect of creating a static method isn't as noticeable as a static field, meaning that no value is changed as a result of accessing it directly, it still designates the field as a member of the type and not a direct instance of a class. The semantics of the static keyword are the same, whereas the behavior is slightly different.

extern designates that the method is defined elsewhere and not within the class itself. It is primarily used to interact with system DLLs in conjunction with the DllImport attribute.

The virtual keyword figures in class inheritance, which is covered later in this chapter. A virtual method means that the method can have its behavior changed by overriding it in a derived class. In other words, if someone wants to change how a class works, he can use the class you created as a base and redefine any methods that are declared virtual. Furthermore, when you declare a method using the Override modifier, you are redefining a method that was declared virtual in the base class you are deriving from. You might in fact hide this overridden method from the person using your derived class by using the new modifier. If you do so, the base class's method will be called rather than the overridden method unless the client who uses the derived class fully qualifies the method name with the derived class name.

The method itself can also specify modifiers on each parameter. By default, a value parameter is passed by value, which means a copy is created and placed on the stack. Any changes that your method makes to that value do not affect the original instance that was passed because you are only working with a copy of that variable. If you use the ref modifier on a parameter, any changes to that parameter change the variable that the client passed to the method. In Listing 2.2, you can see two versions of a method. The first version does not contain the ref modifier on the parameter, so any changes to that parameter do not affect the original variable. In the second version, however, the original variable changes.

Listing 2.2 Reflecting Parameter Changes by Using the ref Modifier
 namespace _3_Methods {     public class Television     {         // current channel tv is set to         private static int channel = 2;         private const int maxChannels = 200;         // changes the channel to a specified channel         public bool ChangeChannel(int newChannel)         {             // only supports 200 channels             if( newChannel > maxChannels )                 return false;             // set private channel variable             channel = newChannel;             return true;         }         public void GetChannel( int param )         {             param = channel;         }         // overloaded version. Note ref parameter         public void GetChannel( ref int param )         {             param = channel;         }     }     class Class1     {       [STAThread]       static void Main(string[] args)       {             // create Television object             Television tv = new Television();             int refChan = 0;             int chan = 0;             // chan remains 0 after call             tv.GetChannel( chan );             // refChan's value will change             tv.GetChannel( ref refChan );             Console.WriteLine( "noref={0} ref={1}", chan, refChan );       }     } } 

The out modifier is similar to ref . It is also passed by reference rather than value, which means it also changes the original value passed into it. The key difference is that you do not use it to pass data from the client to the method, which means there is no guarantee that the parameter is initialized when it is used within the called method. If you pass a variable using the ref keyword without first initializing it to a value, the compiler generates an error. However, if the parameter is an out parameter, then you need not initialize it. Using out is advantageous as it allows a method to return multiple values because you might have more than one out parameter in a single method.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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