Working with Modules


Working with Modules

As you write longer programs, you're likely to have several forms and event procedures that use some of the same variables and routines. By default, variables are local to an event procedure—they can be read or changed only within the event procedure in which they were created. You can also declare variables at the top of a form's program code and give the variables a greater scope throughout the form. However, if you create multiple forms in a project, the variables declared at the top of a form are valid only in the form in which they were declared. Likewise, event procedures are by default declared as private and are only local to the form in which they are created. For example, you can't call the Button1_Click event procedure from a second form named Form2 if the event procedure is declared to be private to Form1. (You'll learn how to add additional forms to your project in Chapter 14, “Managing Windows Forms and Controls at Run Time.”)

To share variables and procedures among all the forms and event procedures in a project, you can declare them in one or more modules included in the project. A module is a special file that has a .vb file name extension and contains variable declarations and procedures that can be used anywhere in the program. (In Visual Basic 6, standard modules have a .bas extension.)

Like forms, modules are listed separately in Solution Explorer and can be saved to disk by using the Save ModuleName.vb As command on the File menu. (ModuleName.vb will match your project.) Unlike forms, modules contain only code and don't have a user interface. And although modules have some similarities with classes (formerly called class modules), they are unlike classes in that they are not object-oriented, do not define the structure and characteristics of objects, and cannot be inherited. (You'll learn more about creating classes in Chapter 16, “Inheriting Forms and Creating Base Classes.”)

Creating a Module

To create a new module in a program, you click the Add New Item button on the Standard toolbar or click the Add New Item command on the Project menu. You can also click the Add Module command on the Project menu.) A dialog box opens, in which you select the Module template and specify the name of the module. A new, blank module then appears in the Code Editor. The first module in a program is named Module1.vb by default, but you can change the name by right-clicking the module in Solution Explorer and typing a new name or by using the Save Module1.vb As command on the File menu. Try creating an empty module in a project now.

Create and save a module

  1. Start Visual Studio, and create a new Visual Basic Windows Application project named My Module Test.

    The new project is created, and a blank form appears in the Designer.

  2. Click the Add New Item command on the Project menu.

    The Add New Item dialog box appears.

  3. Select the Module template.

    The default name, Module1.vb, appears in the Name text box.

    graphic

    TIP
    The Add New Item dialog box offers several templates that you can use in your projects. Each template has different characteristics and includes starter code to help you use them. Visual Studio 2005 includes many new and updated Microsoft Windows forms templates, including Explorer Form, Splash Screen, and Login Form, plus numerous class-related templates. You'll use these templates after you read the introductory material about object-oriented programming in Chapter 16.

  4. Click the Add button.

    Visual Basic adds Module1 to your project. The module appears in the Code Editor, as shown here:

    graphic

    The Method Name list box indicates that the general declarations section of the module is open. Variables and procedures declared in this section are available to the entire project. (You'll try declaring variables and procedures later.)

  5. Double-click the Solution Explorer title bar to see the entire Solution Explorer window, and then select Module1.vb.

    Solution Explorer appears, as shown here:

    graphic

    Solution Explorer lists the module you added to the program in the list of components for the project. The name Module1 identifies the default file name of the module. You'll change this file name in the following steps.

  6. Double-click the Properties window title bar to see the window full size.

    The Properties window displays the properties for Module1.vb, as shown here:

    graphic

    Because a module contains just code, it has only a few properties. By using the most significant property, File Name, you can create a custom file name for the module to describe its purpose. Give this identifying label some thought because later you might want to incorporate your module into another solution. The remaining properties for the module are useful for more sophisticated projects—you don't need to worry about them now.

  7. Change the File Name property to Math Functions.vb or another file name that sounds impressive, and then press Enter. (I'm granting you considerable leeway here because this project is simply for testing purposes—you won't actually create math functions or any other “content” for the module, and later you'll discard it.)

    The filename for your module is updated in the Properties window, Solution Explorer, and the Code Editor.

  8. Return the Properties window and Solution Explorer to their regular docked positions by double-clicking their title bars.

As you can see, working with modules in a project is a lot like working with forms. In the next exercise, you'll add a public variable to a module.

TIP
To remove a module from a project, click the module in Solution Explorer, and then click the Exclude From Project command on the Project menu. Exclude From Project doesn't delete the module from your hard disk, but it does remove the link between the specified module and the current project. You can reverse the effects of this command by clicking the Add Existing Item command on the File menu, selecting the file that you want to add to the project, and then clicking Open.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

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