Creating Procedures


Creating Procedures

Procedures provide a way to group a set of related statements to perform a task. Visual Basic includes two primary types of procedures:

  • Function procedures are called by name from event procedures or other procedures. Often used for calculations, function procedures can receive arguments and always return a value in the function name.

  • Sub procedures are called by name from event procedures or other procedures. They can receive arguments and also pass back modified values in an argument list. Unlike functions, however, Sub procedures don't return values associated with their particular Sub procedure names. Sub procedures are typically used to receive or process input, display output, or set properties.

Function procedures and Sub procedures can be defined in a form's program code, but for many users, creating procedures in a module is more useful because then the procedures have scope throughout the entire project. This is especially true for procedures that might be called general-purpose procedures—blocks of code that are flexible and useful enough to serve in a variety of programming contexts.

Advantages of General-Purpose Procedures

General-purpose procedures provide the following benefits:

  • They enable you to associate an often-used group of program statements with a familiar name.

  • They eliminate repeated lines. You can define a procedure once and have your program execute it any number of times.

  • They make programs easier to read. A program divided into a collection of small parts is easier to take apart and understand than a program made up of one large part.

  • They simplify program development. Programs separated into logical units are easier to design, write, and debug. Plus, if you're writing a program in a group setting, you can exchange procedures and modules instead of entire programs.

  • They can be reused in other projects and solutions. You can easily incorporate standard-module procedures into other programming projects.

  • They extend the Visual Basic language. Procedures often can perform tasks that can't be accomplished by individual Visual Basic keywords or Microsoft .NET Framework methods.

For example, imagine a program that has three mechanisms for printing a bitmap on different forms: a menu command named Print, a Print toolbar button, and a drag-and-drop printer icon. You could place the same printing statements in each of the three event procedures, or you could handle printing requests from all three sources by using one procedure in a module. General-purpose procedures save you typing time, reduce the possibility of errors, make programs smaller and easier to handle, and make event procedures easier to read.



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