Understanding Class Members


A class is a place to store the code you write. Before you can begin writing Visual 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 classan object created from a class using the keyword new. On the other hand, static 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: The Basics," 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. Visual 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).

Although you could place all of your program's code into a single class module, it's best to create different modules to group related 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, preferably, in a specialized class module.

By the Way

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 16, but this is a very advanced topic so I won't be covering it in detail. I highly recommend that you read a book dedicated to the topic of object-oriented programming, 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 creating methods is that you should build classes to group related sets of code. This is not 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.

You're going to be building on the Picture Viewer application from the Hour 9, "Adding Menus and Toolbars to Forms," so open that now.

If you wanted to create an entirely new class, you would choose Add Class from the Project menu. However, for this example, you're going to create methods in your form class.

Start by clicking frmViewer.cs in the Solution Explorer, and then clicking the View Code button that appears at the top of the Solution Explorer. You are now viewing the code of the frmViewer.cs class (see Figure 10.1). The arrow in the figure shows the opening brace below the class definition. This brace denotes the start of the class code; there's a closing brace at the bottom as well. Notice that all of the event procedures that you've created exist between the opening and closing brace of the class. The methods you create will also be placed between these braces.

Figure 10.1. You create your methods within a class.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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