Section 7.2. Modules, Classes and Methods


7.2. Modules, Classes and Methods

Programs consist of many pieces, including modules and classes. These modules and classes are composed of smaller pieces called methods. You combine new modules and classes with "prepackaged" classes available in the .NET Framework Class Library (FCL) and in various other class libraries. Related classes are typically grouped into namespaces (and compiled into library files) so that they can be imported into programs and reused. You will learn how to package your own classes into class libraries in Section 9.13. The FCL provides a rich collection of classes and methods for performing common mathematical calculations (Chapter 3), error checking (Chapter 12), building sophisticated GUI applications (Chapters 13 and 14), string and character manipulations (Chapter 16), graphics (Chapter 17), input/output operations (Chapter 18), XML processing (Chapter 19), database manipulations (Chapter 20), creating applications for the Web (Chapters 21 and 22) and many other useful operations. This framework makes your job easier, because the methods provide many of the capabilities you need. In earlier chapters, we introduced some FCL classes, such as Console, which provides methods for inputting and outputting data and MessageBox, used to display message dialogs.

Software Engineering Observation 7.1

When possible, use .NET Framework classes and methods instead of writing new classes and methods. This reduces program development time and can prevent the introduction of errors.


Performance Tip 7.1

.NET Framework Class Library methods are written to perform efficiently.


You can create your own classes and methods to meet the unique requirements of a particular application. Two types of methods exist: subroutines and functions. We discuss the differences between subroutines and functions shortly. Throughout this book, the term "method" refers to both subroutines and functions unless otherwise noted.

You write methods to define specific tasks that a program may use one or more times during its execution. Although the same method can be executed from multiple points in a program, the actual statements that define the method are written only once.

Let's briefly review some of the basics of methods. A method is invoked (i.e., made to perform its designated task) by a method call. The method call specifies the method name and provides information (as arguments) that the callee (i.e, the method being called) requires to do its job. When the method completes its task, it returns control to the caller (i.e., the calling method). In some cases, the method also returns a result to the caller.

There are several motivations for dividing code into methods. First, the divide-and-conquer approach makes program development more manageable. Another motivation is software reusabilitythe ability to use existing methods as building blocks for new programs. When proper naming and definition conventions are applied, programs can be created from standardized pieces that accomplish specific tasks, to minimize the need for customized code. A third motivation is to avoid repeating code in a programwhen code is packaged as a method, the code can be executed from various points in a program simply by calling the method.

Software Engineering Observation 7.2

To promote reusability, the capabilities of each method should be limited to the performance of a single, well-defined task, and the name of the method should express that task effectively.


Software Engineering Observation 7.3

If you cannot choose a concise method name that expresses the task performed by a method, the method could be attempting to perform too many diverse tasks. Consider dividing such a method into several smaller methods.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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