Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

248 Part II Programming Fundamentals


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.

Like forms, modules are listed separately in Solution Explorer. Unlike forms, modules contain
only code and don’t have a user interface. And although modules have some similarities
with classes, 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, selecting Rename,
and typing a new name. You can also rename a module by changing the File Name property
in the Properties window. Try creating an empty module in a project now.

Create and save a module


  1. Start Microsoft Visual Studio 2010, and then create a new Visual Basic Windows Forms
    Application project named My Module Test.
    The new project is created, and a blank form opens in the Designer.

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


The Add New Item dialog box opens.


  1. Scroll down the list of common templates in the central pane, and then select the
    Module template.
    The default name, Module1 .vb, appears in the Name text box, as shown on the
    following page:


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 includes many useful 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.
Free download pdf