Creating Class Members

   

Creating Class Members

graphics/newterm.gif

A class is a place to store the code you write. Before you can begin writing C# code, you must start with a class. As mentioned in previous hours, a class is used as a template to create an object (which may have properties and/or methods). Properties and methods of classes can be either instance members or static members. Instance members are associated with an instance of a class ”an object created from a class using the keyword new. On the other hand, s tatic members belong to the class as a whole, not to a specific instance of a class. You've already worked with one class using instance members to create a form (refer to Hour 5, "Building Forms ”Part I, for more information).When you double-click an object on a form, you access events that reside in the form's class module.

Other languages, such as Visual Basic, differentiate between class methods and public methods that are globally available outside of a class. C# requires all methods to exist in the context of a class, but a globally available method can be achieved by defining static methods in your class. Static methods are always available regardless of whether an instance of the class exists. In fact, you can't access a static member through an instance of a class, and attempting to do so results in an exception (error).

graphics/bookpencil.gif

Classes are used as templates for the instantiation of objects. I discuss the specifics of creating objects in Hour 17, "Designing Objects Using Classes." Most of the techniques discussed in this hour apply to class modules with instance members (methods that are part of an instantiated object), but I'm going to focus this discussion on static members because they are easier to use (you can create and use static methods without getting into the complications of creating objects).

Although you could place all your program's code into a single class module, it's best to create different modules to group different sets of code. In addition, it's best not to place code that isn't specifically related to a form within a form's class module; place such code in the logical class or in a specialized class module.

graphics/bookpencil.gif

The current development trend centers on object-oriented programming, which revolves around class modules. I'll give you a primer on object-oriented programming in Hour 17, but this is a very advanced topic so I won't be covering it in detail. I highly recommend that you read a dedicated object-oriented book, such as Sams Teach Yourself Object-Oriented Programming in 21 Days , after you are comfortable with the material in this book.

One general rule for using static members is that you should create classes to group related sets of code. This isn't to say you should create dozens of classes. Rather, group related methods into a reasonably sized set of classes. For instance, you might want to create one class that contains all your printing routines and another that holds your data-access routines. In addition, I like to create a general-purpose class in which to place all the various routines that don't necessarily fit into a more specialized class.

Start C# now and create a new Windows Application project named Static Methods.

Change the name of the default form to fclsExample, set its Text property to Method Example, and set the Main() entry point of the project to reference fclsExample instead of Form1. Change the Size property of the form to 371, 300. Next, add a new class to the project by choosing Add Class from the Project menu. C# then displays the Add New Item dialog box, as shown in Figure 11.1.

Figure 11.1. All new project items are added using this dialog box.

graphics/11fig01.jpg


Note that this is the same dialog box used to add new forms. Change the name of the class to clsStaticExample.cs and click Open. C# then creates the new class and positions you in the code window ”ready to enter code (see Figure 11.2).

Figure 11.2. Classes have no graphical interface, so you always work with them in the code editor.

graphics/11fig02.jpg


Save your project now by clicking Save All on the toolbar.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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