vel15.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,355 行 · 第 1/3 页

HTM
1,355
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel15.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel16.htm"><LINK REL="Previous" HREF="velp07.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel15.htm</H2><P ALIGN=LEFT><A HREF="velp07.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel16.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E112" >What You'll Learn</A><LI><A HREF="#E68E113" >Introducing Subprograms</A><LI><A HREF="#E68E114" >Subroutines: Code Routines</A><LI><A HREF="#E68E115" >External .BAS Modules</A><LI><A HREF="#E68E116" >Functions Procedures</A><LI><A HREF="#E68E117" >Homework</A><UL><LI><A HREF="#E69E104" >General Knowledge</A><LI><A HREF="#E69E105" >Write Code That...</A><LI><A HREF="#E69E106" >Find the Bug</A><LI><A HREF="#E69E107" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E22"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 8, Unit 15</B></FONT></CENTER></H1><BR><A NAME="E67E25"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Subprograms</B></FONT></CENTER></H2><BR><BR><A NAME="E68E112"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Introducing subprograms<BR><BR><LI>Subroutines: code routines<BR><BR><LI>External .BAS modules<BR><BR><LI>Functions procedures<BR><BR></UL><P>By the very nature of the material, this unit presents more theory than many of this book's other units have done. Before you can move up to the next level of Visual Basic programming, you must master certain programming techniques needed for writing large-scale applications.<BR><P>Until now, each unit has added to your Visual Basic language vocabulary. So far, the more you learned, the larger your event procedure got. This unit <I>reduces</I> the size of your event procedures! Starting in this unit, you'll see how to break up your programs into smaller and more numerous procedures than you've seen so far.<BR><BR><A NAME="E68E113"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Introducing Subprograms</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Break your programs into as many small but logical sections as possible. The smaller routines make your programming and subsequent maintenance easier.<BR><P>In many traditional programming languages such as COBOL and FORTRAN, a program is like a long book without chapters: The code goes on and on and the program's length is exceeded only by the boredom programmers face trying to wade through the code hunting down errors. You've learned enough about Visual Basic so far to know that a Visual Basic program consists of a lot more than a long program listing. A Visual Basic program consists of the following:<BR><UL><LI>A form with controls that act as the program's background and user interface<BR><BR><LI>A general-purpose procedure named (general), found in the Code window's Object dropdown list<BR><BR><LI>Event procedures that tie the controls together and add specific direction and calculations to the application<BR><BR><LI>A CONSTANT.TXT file that provides named constants used by your code<BR><BR></UL><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The (general) procedure in any program's Code window is often called the <I>declarations section</I>.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>This unit will expand on the purpose of the (general) procedure and will explain how to add different kinds of procedures to your programs.<BR><P>You now know two kinds of procedures: event procedures, which execute as events occur, and the (general) procedure. The (general) procedure is really not an executable procedure in the way that event procedures execute. The only statements that you can place in the (general) procedure are data definition statements such as Dim statements and option statements such as these:<BR><BR><PRE><FONT COLOR="#000080">Option Base 1</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">Open Explicit</FONT></PRE><P>Many Visual Basic programmers don't worry about using the Option Base 1 statement, which, as you learned in Lesson 6, specifies that all array subscripts will begin at 1 rather than 0 (the default). Rather than use Option Base 1, most programmers just ignore the first subscript of 0 and act as if the subscripts in all arrays begin at 1.<BR><P>The Option Explicit statement is a helpful statement that you can place in the (general) procedure to tell Visual Basic to look for any undefined variables and to issue an error if Visual Basic finds any. By requiring that you explicitly define all variables before you use them, misspelled variable names almost never cause the problem they would otherwise.<BR><P>Assume that your program does <I>not</I> include the Option Explicit statement and you type the following in an event procedure:<BR><PRE><FONT COLOR="#000080">Dim Sales As CurrencySale = 294.43lblOut.Caption = Sales ' Outputs zero</FONT></PRE><P>Visual Basic outputs zero in the label because Sales contains zero, and the variable that you thought was named Sales, which you accidentally named Sale, holds the value of 294.43, which the user won't see.<BR><P>If you were to add the Option Explicit statement in the (general) procedure (the only place you can put Option statements), Visual Basic would display the error message box shown in Figure 15.1, because Visual Basic correctly deduces that you have yet to define Sale but you're trying to store a value in Sale.<BR><P><B> <A HREF="15vel01.gif">Figure 15.1. Define all variables before using them or you'll get this error.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>Variable <I>declaration</I>, in Visual Basic, means the same thing as variable <I>definition</I>.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The Options Environment menu displays a list of options that contain a Require Variable Declaration option that you can set to True. If a program's (general) procedure includes the Option Explicit statement, the program requires that all variables be defined before their use no matter how the Options Environment menu is set.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>There are additional procedures that you can place in your programs. The general name for these procedures is <I>subprogram</I>. These procedures, just like the event procedures, are like small versions of a Visual Basic program. In a way, all procedures are the building blocks of the overall application code because all the procedures work together to comprise the complete application.<BR><P>There are two kinds of Visual Basic subprograms: <I>subroutine procedures</I> and <I>function procedures</I>. Often, we abbreviate <I>subroutine procedures</I> to just <I>subroutines</I> and we abbreviate <I>function procedures</I> to <I>functions</I>. This may cause confusion due to the built-in functions such as Int() that you read about in the previous lesson. This book reserves the term <I>function procedure</I>, or just <I>function</I>, for the function procedures that you write, and this book refers to the functions supplied by Visual Basic as the <I>built-in functions,</I> as has been the case throughout the earlier lessons.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Event procedures are actually specialized subroutine procedures. It's important, however, to distinguish between event procedures and the general-purpose subroutines you'll write that aren't tied to events. Therefore, this book will continue to refer to event procedures by that name, and this book will refer to subroutine procedures either <I>subroutine procedures</I> or just <I>subroutines</I>.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>This section, as well as most of this unit, is concerned with getting the terminology straight that you'll read about and use throughout the rest of this book. Visual Basic programs are actually just small sections of code named <I>subprograms</I> that you write and that appear along with a (general) definitions section. There are two kinds of subprograms: subroutine procedures (which include event procedures) and function procedures. The rest of this unit explains more about these Visual Basic program divisions and also explains how to store subprograms in external files that more than one Visual Basic application can share.<BR><BR><A NAME="E68E114"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Subroutines: Code Routines</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>A subroutine procedure always begins with the Sub statement and always ends with the End Sub statement. A subroutine procedure may or may not be an event procedure. Non-event procedures are general-purpose subroutines that you can write and add to any program.<BR><P>Listing 15.1 contains an event procedure that you've seen in almost every program in this book.<BR><P><FONT COLOR="#000080"><B>Listing 15.1. A common event procedure.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdExit_Click()2: End3: End Sub</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Wrapper lines</I> are lines of code that start and terminate procedure.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Lines 1 and 3, the wrapper lines, confirm that the Exit command button's Click event procedure is a subroutine procedure, as mentioned previously.<BR><P>Event procedures are specific subroutines tied directly to control events. There will be times, many times in fact, when you'll write subroutines that aren't tied to any events whatsoever. When you write a section of code that your application will have to execute more than once, that section of code is a great candidate for a general-purpose, non-event subroutine.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?