📄 vel16.htm
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel16.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp08.htm"><LINK REL="Previous" HREF="vel15.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel16.htm</H2><P ALIGN=LEFT><A HREF="vel15.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="velp08.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="#E68E118" >What You'll Learn</A><LI><A HREF="#E68E119" >Three Kinds of Variable Scope</A><LI><A HREF="#E68E120" >Global Variables</A><LI><A HREF="#E68E121" >Module Variables</A><LI><A HREF="#E68E122" >Local Variables—The Safest Variables</A><LI><A HREF="#E68E123" >Passing Arguments</A><LI><A HREF="#E68E124" >Receiving Two Ways: By Address and By Value</A><LI><A HREF="#E68E125" >Homework</A><UL><LI><A HREF="#E69E108" >General Knowledge</A><LI><A HREF="#E69E109" >Write Code That...</A><LI><A HREF="#E69E110" >Find the Bug</A><LI><A HREF="#E69E111" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E23"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 8, Unit 16</B></FONT></CENTER></H1><BR><A NAME="E67E26"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Arguments and Scope</B></FONT></CENTER></H2><BR><BR><A NAME="E68E118"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Three kinds of variable scope<BR><BR><LI>Global variables<BR><BR><LI>Module variables<BR><BR><LI>Local variables—the safest variables<BR><BR><LI>Passing arguments<BR><BR><LI>Receiving two ways: by address and by value<BR><BR></UL><P>Now that you can write subprograms—including subroutine and function procedures stored in modules—it's time to learn how those routines can communicate with each other. Until now, all variables that you've defined have been <I>local variables</I>. A local variable is known only within the procedure in which you define the variable. Therefore, if one procedure calculated a variable and a second procedure needed to work with that same variable, the two procedures would have no way to share that variable in a way that both could work with the variable.<BR><P>Until now, if two or more procedures had to work with a value, that value had to be a value from a control on the form. Controls are available to all procedures within the application. There won't always be controls for all values, however, especially the intermediate values needed in many large applications. Only those values displayed to the user should be stored in controls. The remaining intermediate values that you'll work with must stay in variables and be available to all procedures that work with those variables.<BR><BR><A NAME="E68E119"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Three Kinds of Variable Scope</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are three kinds of variable scope: local, module, and global. This section explains how to define variables that fit in any of those three kinds of scope.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Scope</I> determines how much of a program can access a variable.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>You already know about all the data types that variables can take on. There are integers, strings, and decimal variables, as well as variant variables that can accept any data type. Not only do variables have names, contents, and data types, but variables also take on one of these three kinds of scope:<BR><UL><LI>Local variable scope: All variables that you define within a procedure have local variable scope. These variables are known as <I>local variables</I>. Local variables are usable and available only from code within their own procedures. The Dim, Static, and ReDim statements all define local variables. This book describes only Dim for local variables because of Dim's widespread use.<BR><BR><LI>Module variable scope: Each module, including your regular form module as well as any modules that you create or add to your program's Project window, has a (general) procedure in which you can define variables using Dim that have <I>module</I> scope. These variables are known as <I>module</I> variables. Module variables are usable and available to all procedures within that module. Therefore, if you define variables in the (general) procedure of a module file named MYPROCS.BAS, those variables are module variables, and any procedure within that module can access those variables. Nevertheless, procedures within the form's regular Code window module can't access the other module's variables.<BR><BR><LI>Global variable scope: Variables that you define using the Global statement inside the (general) procedure of any non-form module have <I>global</I> scope. These variables are known as <I>global</I> variables. When needed, this book will make the distinction between the form's module (the Code window that all forms provide) and additional modules that you add or create.<BR><BR></UL><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>If you were to look at CONSTANT.TXT, the module file that you added to AUTOLOAD.MAK, you would see that all of its named values are defined inside the (general) procedure. The location of those definitions and their definition using the Global statement dictate that all values inside CONSTANT.TXT are defined at the module level. If Microsoft defined all of CONSTANT.TXT's named constant values from within a CONSTANT.TXT procedure other than (general), no other procedure could access the values within CONSTANT.TXT.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When you need to define variables, you should no longer automatically use the Dim statement. Now that you know how to write programs that contain several procedures, one or more of which might need to share data, you'll need to decide not only <I>how</I> you want to define the variables (using Dim or Global), but also <I>where</I> you want to define those variables. Your choice depends on the scope that the variables need. If a variable is used only within a single procedure, there is no reason to define that variable to have module or global scope.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The scope of data determines the extent to which other parts of the program can access that data. Controls always have global scope because any procedure in any of a program's modules can use control data. Variables defined in a module's (general) procedure using the Global keyword are also global and available from everywhere in the program. Variables defined in the (general) procedure using Dim are module variables and are available from any procedure in the module. Variables defined using Dim inside procedures (other than the (general) procedure) are local variables and are available only within that procedure.<BR><BR><A NAME="E68E120"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Global Variables</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Global statement defines global variables. You can use Global only within a module's (general) procedure. Often, programmers add the Const keyword, as done throughout CONSTANT.TXT, to define named constant values that are variables whose values can't change.<BR><P>The Global statement is similar to Dim. Here is the format of Global:<BR><BR><PRE><FONT COLOR="#000080">Global [Const] <I>VarNa me</I> [AS <I>DataType</I>] [= <I>value</I>]</FONT></PRE><P>You can define variables only as global variables in the (general) declarations procedure of a module. Every procedure within that entire application can access the global variables. Therefore, a module's Form_Load() procedure can initialize a global variable that you defined, and another module's function procedure can access and change that same variable. If you use the Const keyword, you must also assign the variable an initial value but <I>not</I> use a data type. Without Const, you must specify the data type.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Define constant names that contain all uppercase letters. Throughout the program, you'll more easily be able to distinguish variable from constant names.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following statement, appearing in the (general) procedure of any non-form module, defines an integer global variable named MyGlobal. Any procedure within the entire application can initialize, access, and change the variable.<BR><BR><PRE><FONT COLOR="#000080">Global MyGlobal As Integer</FONT></PRE><P>The Const keyword enables you to name constants that you'll use throughout the rest of the program. For example, if your company's number of divisions is eight, you might want to define a global named constant like this:<BR><BR><PRE><FONT COLOR="#000080">Global Const NUMDIVS = 8</FONT></PRE><P>A named constant can <I>never</I> be changed, by user input, an assignment, or through any other kind of variable-changing statement for the rest of the program. NUMDIVS will always hold the value of 8. The advantage of using a named constant over the value of 8 everywhere in the program that needs to use the number of divisions is that, if the number of divisions change, you have to change the number in only one place, and the rest of the program automatically uses the new value. Also, the Const specifier keeps you or someone who modifies your code from accidentally overwriting the value elsewhere in the code. Figure 16.1 shows the error message box that Visual Basic displays if any line in the program attempts to change a named constant.<BR><P><B> <A HREF="16vel01.gif">Figure 16.1. Visual Basic reminds you if you attempt to change a named </B><B>constant.</A></B><BR><P>The reason that you must specify an initial constant value is because the Global statement is the <I>only</I> place in a program where you can assign values to constants.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 16.1 contains three global variable and two global named constant definitions. The code must appear in the (general) procedure of a non-form module or Visual Basic will issue an error message.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Global statement enables you to define global variables. If you use the Const modifier to name global constants, you can't define the global's data type, but you must initialize the global constant at the time that you define the named constant.<BR><P><FONT COLOR="#000080"><B>Listing 16.1. Defines five global values.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Option Explicit2:3: ' Three global variables4: Global G1 As Single5: Global G2 As Double6: Global G3 As Single7:8: ' Two named constants9: Global Const G4 = 495.4210: Global Const G5 = 0</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 specifies that all variables within the module must be explicitly defined. The Option Explicit statement means that there's little chance that you'll misspell a variable name. Lines 4 through 6 define three global variables. Any procedure in the entire program, even procedures in other modules, can initialize, read, and change G1, G2, and G3. Lines 9 and 10 define two globally named constant values called G4 and G5. Both of the constants are defined in lines 9 and 10 because G4 and G5 can't be initialized anywhere else in the program.<BR><BR><A NAME="E68E121"></A><H3 ALIGN=CENTER><CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -