Reusing Functions and Procedures

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 8.  Managing Program Tasks with Procedures


As you have seen in the examples in this chapter, you can create a procedure in either of two places: a form or a module. Where you place the procedure depends on where you need to use it and what its purpose is. If the procedure is specific to a form or modifies the properties of the form or its associated controls, you should probably place the procedure in the form itself.

If, on the other hand, you are using the procedure with multiple forms in your program or have a generic procedure used by multiple programs, you should place it in a module. The storage location of your procedure is determined by where you create it. If you want, you can move a procedure from a form to a module or vice versa using cut-and-paste editing or even drag-and-drop editing.

Storing a Procedure in a Form File

Note

In Visual Basic .NET, form files and modules are both types of classes.


To create a procedure in a form file, you just need to choose the form from the Solution Explorer window and then access the code for the form. You do so either by double-clicking the form itself (or any control) or choosing the View Code button in the Solution Explorer window. After the Code window appears, you create a procedure as described in the earlier section "Creating a Sub Procedure."

Using a Module File for Procedures

A module file contains only code no form elements or events. If you already have a module file in your project, you can create a new procedure by selecting the file, opening the Code window, and then using the steps listed earlier to build the procedure.

Tip

Double-clicking the module name in the Solution Explorer window automatically opens the Code window for the module.


If you don't have a module file in your project, or if you want to use a new module, you can create a module by selecting Project, Add Module. You can also create a new module by clicking the arrow on the Add New Item button in the toolbar and then choosing Add Module from the drop-down menu. Either way, you are presented with the Add Module dialog box; select the Module icon, give it a filename, and click Open. A new module is created, and the Code window appears for you to begin editing.

Note

The toolbar button for adding new forms and modules is a drop-down button, which means clicking on the arrow gives you a list of items. After an item has been selected, the icon on the button changes.


Tip

If you have a library of common functions, such as printing routines, keep them in a separate module file so that you can easily add the library to different projects.



    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

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