⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vel16.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Receiving Two Ways: By Address and By Value</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are two ways to receive passed arguments: by address and by value. The method that you use determines whether the receiving procedure can change the arguments so that those changes remain in effect after the calling procedure regains control. If you pass and receive by address (the default method), the calling procedure's passed local variables may have been changed in the receiving procedure. If you pass and receive by value, the calling procedure can access and change its received arguments, but those changes don't retain their effects in the calling procedure.<BR><P>Subroutines and functions can always use their received values and also change those arguments. If a receiving procedure changes one of its arguments, the corresponding variable in the calling procedure <I>is </I><I>also changed</I>. Therefore, when the calling procedure regains control, the value (or values) that the calling procedure sent as an argument to the called subroutine may be different than before the call.<BR><P>Arguments are passed by <I>address,</I> meaning that the passed arguments can be changed by their receiving procedure. If you want to keep the receiving procedure from being able to change the calling procedure's arguments, you must pass the arguments by <I>value.</I> To pass by value, precede any and all receiving argument lists with the ByVal keyword, or enclose the passed arguments in parentheses.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>It's generally safer to receive arguments by value because the calling procedure can safely assume that its passed values won't be changed by the receiving procedure. Nevertheless, there may be times when you want the receiving procedure to permanently change values passed to it, and you'll need to receive those arguments by address.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 16.5 shows two subroutine procedures. One, named Changes(), receives its arguments by address. The second procedure, NoChanges() receives its arguments by value. Even though both procedures multiply their arguments by ten, those changes affect the calling procedure's variables only when Changes() is called but not when NoChanges() is called.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The method by which you send and receive arguments determines how long a receiving procedure's changes stay in effect. If the receiving procedure changes one or more of its received-by-address arguments, those changes remain in effect when the calling procedure regains control. Therefore, when calling a procedure that accepts arguments passed and received by address, be aware that the passed values could be different when control returns to the calling procedure.<BR><P><FONT COLOR="#000080"><B>Listing 16.5. One procedure receives by address and the other receives by value.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Changes (N As Integer, S As Single)2: ' Receives arguments by address3: N = N * 2 ' Double both4: S = S * 2 ' arguments5: ' When the calling routine regains control,6: ' its two local variables will now be twice7: ' as much as they were before calling this.8: End Sub9: 10: Sub NoChanges (ByVal N As Integer, ByVal S As Single)11: ' Receives arguments by value12: N = N * 2 ' Double both13: S = S * 2 ' arguments14: ' When the calling routine regains control,15: ' its two local variables will not be16: ' changed from their original values.17: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 defines the Changes() procedure to receive two arguments by address. Therefore, when lines 3 and 4 double those two values, the calling procedure's variables will be doubled also.<BR><P>Line 10 defines the NoChanges() procedure that receives its two arguments by value. The ByVal keyword tells Visual Basic that no matter what happens to the arguments in the NoChanges() procedure, the calling procedure's values will be unaffected by the change.<BR><P>The following statements would call these two procedures properly:<BR><PRE><FONT COLOR="#000080">Call Changes(N, S)Call NoChanges(X, S)</FONT></PRE><P>Again, the variable names in the calling procedures do not have to match the corresponding received names in the receiving argument lists. Therefore, the calling procedure sends its local variable named X to NoChanges(), which references that value as N inside the receiving procedure. If you want to send data to <I>either</I> procedure and ensure that the sent variables can't be changed even in the procedure that receives by address, enclose each sent argument in parentheses like this:<BR><PRE><FONT COLOR="#000080">Call Changes( (N), (S) )Call NoChanges( (X), (S) )</FONT></PRE><P>The special parentheses coding overrides any by address passing and ensures that the passed values will retain their original values after the called procedures finish.<BR><BR><A NAME="E68E125"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E108"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is the name of the scope of a variable defined inside a procedure?<BR><BR><LI>What is the name of the scope of a variable defined using the Dim keyword inside a module?<BR><BR><LI>What is name of the scope of a variable defined using the Global keyword?<BR><BR><LI>True or false: You can define a global variable inside the form's module.<BR><BR><LI>True or false: You can define a module variable inside the form's module.<BR><BR><LI>True or false: You can define a module variable inside a non-form module.<BR><BR><LI>Which kind of scope offers the least amount of safety?<BR><BR><LI>Which kind of scope is used the least?<BR><BR><LI>How are the values in CONSTANT.TXT scoped?<BR><BR><LI>What does the Const keyword do?<BR><BR><LI>Why must you assign initial values to named constants when you define named constants?<BR><BR><LI>Where must you define all named constants?<BR><BR><LI>True or false: Your application can have two different variables with the same name.<BR><BR><LI>What happens to local variables when their procedures end?<BR><BR><LI>What is the name of the procedure that sends arguments to another?<BR><BR><LI>What is the name of the procedure that receives arguments from another?<BR><BR><LI>True or false: Built-in functions need no arguments.<BR><BR><LI>Why must you specify data types for each argument in receiving argument lists?<BR><BR><LI>True or false: You sometimes use the Call statement to call function procedures.<BR><BR><LI>True or false: You can pass and receive at most one value to and from function procedures.<BR><BR><LI>How many ways can a procedure receive variables?<BR><BR><LI>Describe what the term <I>by address</I> means to the program.<BR><BR><LI>Describe what the term <I>by value</I> means to the program.<BR><BR><LI>Describe the two ways to ensure that arguments pass by value.<BR><BR><LI>When receiving arrays, what function tells the receiving procedure the highest subscript value available for the array?<BR><BR></OL><BR><A NAME="E69E109"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Define two global variables that you could use to hold a person's last name and age.<BR><BR><LI>Define two global constants that hold the number of days in the week and the number of months in a year.<BR><BR></OL><BR><A NAME="E69E110"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>What's wrong with the following global variable definitions?<BR>Global Const X1 As Integer<BR>Global Const X2 As Integer = 19<BR>Global X3 = 19<BR>Global X4 As Integer = 19<BR><BR><LI>Merle needs to write a function procedure that accepts an integer variable and a string array that contains 45 elements. Merle keeps getting an error with the following function definition statement. Perhaps you can tell Merle what he needs to do.<BR>Function Report(Age As Integer, CoNames(45) As String)<BR><BR></OL><BR><A NAME="E69E111"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><OL><LI>Write a general-purpose function procedure that accepts one string argument and returns a string that contains only every other letter of the passed string. Return a null string, &quot;&quot;, if the passed string contains fewer than 2 characters.<BR><BR></OL><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="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></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></BODY></HTML>

⌨️ 快捷键说明

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