Chapter 10: Creating Modules and Procedures


Chapter 10

Creating Modules and Procedures

After completing this chapter, you will be able to:

  • Employ structured programming techniques and create modules containing public variables and procedure definitions.

  • Practice using public variables that have a global scope.

  • Increase programming efficiency by creating user-defined functions and Sub procedures.

  • Master the syntax for calling and using user-defined procedures.

  • Pass arguments to procedures by value and by reference.

In the first nine chapters of this book, you have used event procedures such as Button1_Click, Timer1_Tick, and Form1_Load to manage events and organize the flow of your programs. In Visual Basic programming, all executable statements must be placed inside some procedure; only general declarations and instructions to the compiler can be placed outside a procedure's scope. In this chapter, you'll continue to organize your programs according to structured programming techniques by developing a hierarchical structure within your application and by breaking computing tasks into discrete logical units.

You'll start by learning how to create modules, which are separate areas within a program that contain global, or public, variables and Function and Sub procedures. You'll learn how to declare and use public variables, and you'll learn how to build general-purpose procedures that save coding time and can be used in more than one project. The skills you'll learn will be especially applicable to larger programming projects and team development efforts.

Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005

If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Microsoft Visual Basic 2005, including the following:

  • Earlier versions of Visual Basic distinguished between standard modules and class modules. In Microsoft Visual Studio 2005, the terms are simply modules and classes.

  • Modules are still supported in Visual Basic 2005, but since Microsoft Visual Basic .NET 2002, it has been necessary to wrap the module content with the keywords Module and End Module in the Code Editor. Public variables are declared in modules as they are in Visual Basic 6.

  • Visual Basic 2005 continues to support the Function and Sub keywords, with which you can create your own procedures. However, the syntax for declaring and calling procedures has changed a little.

  • If you're using the default Option Explicit setting to control variable declaration, a specific type declaration is recommended for functions when you declare them. It's also recommended that you specifically declare all types in your procedure argument lists. If you don't assign a type by using the As keyword, Visual Basic uses the default Object type for the parameter, which is often less efficient than a specific data type.

  • Visual Basic .NET 2002 changed the way that arguments are passed to procedures. In Visual Basic 6, the default mechanism for passing arguments is by reference (ByRef), meaning that changes to arguments in the procedure are passed back to the calling routine. Beginning with Visual Basic .NET 2002 (and continuing in Visual Basic 2005), the default way to pass arguments is by value (ByVal), meaning that changes to arguments within a procedure aren't passed back to the calling routine. You can explicitly specify the behavior for argument passing by using the ByRef and ByVal keyword in your argument declarations. If necessary, you can specify ByRef to achieve the same functionality you have in Visual Basic 6.

  • When you call procedures in Visual Basic 2005, parentheses are required around all argument lists. The Visual Studio IDE adds these for you—even if your procedures don't require any arguments. (If no arguments are required, empty parentheses are inserted.)

  • Programmers now have the option of using the Return statement to send the result of a function calculation back to the calling routine. The older method—assigning a value to the function name—is also supported.



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