A.16. Chapter 16: Building Modular Code and Using Classes

Arrange your code in modules Rather than use a single lengthy, complex procedure that accomplishes many tasks at once, programmers usually subdivide their code into smaller, self-contained procedures—dedicated to a single, discrete task.

Master It Shorter, self-contained, single-task procedures offer the programmer several advantages. Name three.

Solution Advantages of shorter, self-contained, single-task procedures include the following:

  • Modular code is often easier to write because you create a number of short procedures, each of which performs a specific task.

  • You can usually debug shorter procedures relatively easily.

  • Short procedures are more readable because you can more easily follow what they do.

  • By breaking your code into procedures, you can repeat their tasks at different points in a sequence of procedures without needing to repeat the lines of code.

  • By reusing whole procedures, you reduce the amount of code you have to write.

  • If you need to change an item in the code, you can make a single change in the appropriate procedure instead of having to make changes at a number of locations in a long procedure.

  • You can easily reuse short, dedicated, single-task procedures in other code in the future.

Call a procedure You execute a procedure by calling it from within your programming code.

Master It How do you call a procedure?

Solution You can use the optional Call statement, like this, to call a procedure named FormatDocument:

Call FormatDocument

But most programmers omit the Call keyword, using just the name of the procedure, like this:

FormatDocument

Pass information from one procedure to another Sometimes a procedure requires that you pass it some information. For example, a procedure that searches text and makes some style changes to it will require that you pass the text you want modified.

Sometimes a procedure passes back information to the procedure that called it. For example, it might pass back a message describing whether the actions taken in the procedure were (or were not) accomplished successfully.

Master It What kind of procedure can pass back information to the caller?

Solution Only functions can pass back information to a caller. Subroutines can accept data (arguments) like functions, but subroutines cannot pass data back to the caller.

Understand what classes are and what they're for Contemporary computer programs employ classes for various reasons—to help organize large programs, to make code more easily reusable, to provide certain kinds of security, or as a superior substitute for public variables. But beginners sometimes have a hard time wrapping their mind around the concept, particularly the relationship between classes and objects.

Master It What is the difference between a class and an object?

Choose the correct answer (only one answer is correct):

  1. A class is like a cookie and an object is like a cookie cutter.

  2. A class is like a programmer and an object is like a module.

  3. A class is like a blueprint and an object is like a house built from that blueprint.

Solution The answer is 3. A class is like a blueprint and an object is like a house built from that blueprint.

Create an object class The Visual Basic Editor employs a special kind of module for creating classes.

Master It How do you create a class module in the Visual Basic Editor?

Solution There are three ways to create a class module:

  • Right-click the name of the target project in the Project Explorer (or right-click one of the items contained within the project). Then choose Insert Class Module from the context menu.

  • Click the Insert button on the Standard toolbar and choose Class Module from the dropdown list.

  • Choose Insert Class Module from the menu bar.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.111.77