Procedures Defined

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 create more and larger programs, you will often find yourself using the same block of code over and over in several places throughout your program (and in multiple programs). Surely, there must be a better way to handle repetitive code than to just place it in multiple locations in your program, right? Of course there is. The solution is to use procedures. Procedures are segments of code that perform a particular task and then return processing to the area of the code from which they were called. This means that a single procedure can be called from multiple places in your code and, if managed properly, can be used with multiple programs.

You have already been exposed to working with procedures even if you didn't know it. Each time you entered code to be executed by a Button control (or another type of control) in response to an event, you were building a type of procedure known as an event handler. Event handlers are procedures that are automatically called by a program when an event is triggered. As you might already know, you can also create your own procedures and call them when you need them. The procedures that you build are often referred to as user-defined sub procedures (or subroutines). Although the code in earlier examples was entirely contained in event handler procedures, a "real" program might contain a large amount of code in standalone user-defined sub procedures.

Types of Procedures

When writing code for your Visual Basic .NET applications, there are four types of procedures you can create:

  • Sub Procedures perform a specific task. You will learn about sub procedures throughout this chapter. Sub procedures are often known simply as "procedures," and are denoted by the keyword Sub at the beginning of the procedure.

  • Function Procedures return a value to the code that called them. Function procedures are discussed later in this chapter. They are denoted by the keyword Function at the beginning of the procedure.

  • Event-Handling Procedures, or "event handlers," are invoked in response to an event having occurred to an object. Events can be triggered by the user or by the program. You have already worked with event-handling procedures, and will continue to do so throughout this book. Event handlers are normally identified by the name of the object followed by an underscore and the name of the event that invokes the procedure. For example, btnExit_Click denotes the Click event handler for the Button control named btnExit. Regardless of the procedure's name, however, the Handles keyword at the end of the procedure's definition (Handles btnExit_Click would have been at the end of the preceding example) declares which object/event combination is handled by the procedure.

  • Property Procedures are used when assigning values to the properties of user-created objects, and when retrieving the values of those properties. Property procedure definitions include the keyword Property. They are covered in Chapter 16, "Creating Your Own Windows Controls."

All these types of procedures are similar at their most basic level; that is, they are all procedures. The concepts discussed throughout the remainder of this chapter apply to each type.


    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