2.4 Instantiating and Using Objects

 <  Day Day Up  >  

You want to use a class and access its methods .


Technique

Define a variable with the name of the class you want to use as the type name . To create a new instance of that class, use the new keyword followed by the name of the class and a parentheses parameter list. Assign the result of this to your original variable.

Once an object is created, you are free to call its public methods and properties, as shown in Listing 2.3. You do so using the dot ( . ) operator on the object followed by the name of the method and its associated parameters. However, if the method you want to call is a static method, recall that that method belongs to the type and not a specific instance of an object. Therefore, you must use the dot operator on the name of the class rather than the variable name of the object.

Listing 2.3 Creating and Using a Class
 using System; namespace _4_UsingObjects {     public class Television     {         public Television(){}         private int channel = 2;         private static bool on = false;         // increments channel if tv is on         public void ChannelUp()         {             if( on == false )                 return;             ++channel;         }         // decrements channel if tv is on         public void ChannelDown()         {             if( on == false )                 return;             --channel;         }         // gets or set current channel         public int Channel         {             get             {                 return channel;             }             set             {                 channel = value;             }         }         // get status of TV and allows you to         // turn it on or off         public static bool On         {             get             {                 return on;             }             set             {                 on = value;             }         }     }     class Class1     {         [STAThread]         static void Main(string[] args)         {             Television tv = new Television();             // turn television on             Television.On = true;             // change channel             tv.Channel = 42;             Console.WriteLine( "Channel = {0}", tv.Channel );             tv.ChannelUp();             Console.WriteLine( "Channel = {0}", tv.Channel );             tv.ChannelDown();             Console.WriteLine( "Channel = {0}", tv.Channel );         }     } } 

Comments

If you have been following the code for this book, you might have already noticed how to instantiate a new object using the new keyword. However, a few things have gone unsaid up to this point.

When you use the new keyword to create a new instance of a class, the class name following the keyword is not just the name of the class but also signifies which constructor you want to call to create that class. Because overloaded constructors appear later in this chapter, you have been using the default constructor, which does not have any additional parameters. With an overloaded constructor, you pass in additional values to initialize the object.

 <  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