vel07.htm

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

HTM
2,743
字号
<BR><P>As you will see throughout this book, you use variables to hold intermediate mathematical results, as well as data values typed by the user in controls such as text boxes. Variables work well with the controls on the form. So far, you have learned how to define variables but not store data in them. The next section explains how to store and retrieve values from the variables that you define.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>If you begin calling a variable one name, you must stay with that name for the entire program. Sale is not the same variable name as Sales. If you define a variable using a name and later change only the case of certain letters, Visual Basic will convert the original case to the new case. In other words, if you define a variable with the name Profit and use that name for a while in the program, but later in the program you call the variable PROFIT, Visual Basic changes all previous occurrences of Profit to PROFIT.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Visual Basic supports a shortcut when you need to define several variables. Instead of listing each variable definition on separate lines like this:<BR><PRE><FONT COLOR="#000080">Dim A As IntegerDim B As DoubleDim C As IntegerDim D As StringDim E As String</FONT></PRE><P>you can combine the variables of the same data type on one line. For example,<BR><PRE><FONT COLOR="#000080">Dim A, C As IntegerDim B As DoubleDim D, E As String</FONT></PRE><P>Some programmers even code all variable definitions on the same line. As you can see, though, too many variable definitions makes Dim hard to manage. For example,<BR><BR><PRE><FONT COLOR="#000080">Dim A, C As Integer, B As Double, Dim D, E As String</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Reducing Dim with Variable Suffix Characters: There is a set of suffix characters for variable names that you can use to specify a variable's data type without having to define the variable first. Table 7.2 listed the suffix characters for constant values. Variables use the more complete version shown here:</NOTE><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="50%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Suffix</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Data Type</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Example</FONT></TABLE><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>%</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>integer</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Age%</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&amp;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>long</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Amount&amp;</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>!</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>single</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Distance!</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>#</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>double</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>KelvinTemp#</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>@</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>currency</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Pay@</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>$</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>string</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>LastName$</FONT></TABLE><BR><NOTE>The statement Option Explicit cannot appear in the (general) section of the Code window because the suffix characters are not enough to define variables.<BR>The variable LastName$ is a string variable, and Visual Basic knows that the variable is a string variable even if no Dim statement defines the variable as a string. You won&#146;t use the variable suffixes much because you will write clearer programs if you use Dim. Nevertheless, you might run into code written by others that uses the suffix characters on variables, and you should be aware of what the suffix characters mean.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E50"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Assigning Values to Variables</B></FONT></CENTER></H4><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>null string</I> is a zero-length empty string that is often represented as &quot;&quot;.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When you first define variables, Visual Basic stores zeroes in the numeric variables and null strings in the string variables. Use the <I>assignment statement</I> when you want to put other data values into variables. Variables hold data of specific data types, and many lines inside a Visual Basic program's Code window consist of assignment statements that assign data to variables. Here is the format of the assignment statement:<BR><BR><PRE><FONT COLOR="#000080">[Let]VarName = Expression</FONT></PRE><P>The Let command name is optional; it is rarely used these days. The <I>VarName</I> is a variable name that you have defined using the Dim statement. <I>Expression</I> can be a constant, another variable, or a mathematical expression.<BR><P>Suppose that you need to store a minimum age value of 18 in an integer variable named MinAge. The following assignment statements do that:<BR><BR><PRE><FONT COLOR="#000080">Let MinAge = 18</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">MinAge = 18</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The rest of this book omits Let from assignment statements because the word is superfluous.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>To store a temperature in a single-precision variable named TodayTemp, you could do this:<BR><BR><PRE><FONT COLOR="#000080">TodayTemp = 42.1</FONT></PRE><P>The data type of <I>Expression</I> must match the data type of the variable to which you are assigning. In other words, the following statement is invalid. It would produce an error in Visual Basic programs if you tried to use it.<BR><BR><PRE><FONT COLOR="#000080">TodayTemp = &quot;Forty-Two point One&quot;</FONT></PRE><P>If TodayTemp were a single-precision variable, you could not assign a string to it. However, Visual Basic often makes a quick conversion for you when the conversion is trivial. For example, it is possible to perform the following assignment even if you have defined Measure to be a double-precision variable:<BR><BR><PRE><FONT COLOR="#000080">measurement = 921.23</FONT></PRE><P>At first glance, it appears that 921.23 is a single-precision number because of its size. 921.23 is actually a variant data value. Recall that Visual Basic assumes all data constants are variant unless you explicitly add a suffix character to the constant to make the constant a different data type. Visual Basic can easily and safely convert the variant value to double-precision. That's just what Visual Basic does, so the assignment works fine.<BR><P>In addition to constants, you can assign other variables to variables. Consider the following code:<BR><PRE><FONT COLOR="#000080">Dim Sales As Single, NewSales As SingleSales = 3945.42NewSales = Sales</FONT></PRE><P>When the third statement finishes, both Sales and NewSales have the value 3945.42.<BR><P>Feel free to assign variables to controls and controls to variables. Suppose, for example, that the user types the value 18.34 in a text box's Text property. If the text box's Name property is txtFactor, the following statement stores the value of the text box in a variable named FactorVal:<BR><BR><PRE><FONT COLOR="#000080">FactorVal = txtFactor.Text</FONT></PRE><P>Suppose that you defined Title to be a string variable with a fixed length of 10, but a user types Mondays Always Feel Blue in a text box's Text property that you want to assign to Title. Visual Basic stores only the first ten characters of the control to Title and truncates the rest of the title. Therefore, Title holds only the string &quot;Mondays Al&quot;.<BR><P><FONT COLOR="#FF8000"><B><I>Stop &amp; Type:</I></B></FONT> This is the first of several program code reviews that you will find throughout the rest of the book. Listing 7.1 contains a short event procedure that assigns a new command button caption.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>You can instantly make data appear on the form by assigning the Text property of text boxes or the Caption property of labels and command buttons. Suppose you put a command button named cmdJoke on a form. The event procedure shown in Listing 7.1 changes the command button's caption when the user clicks the command button.<BR><P><FONT COLOR="#000080"><B>Listing 7.1. An event procedure that assigns a new command button </B><B>caption.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdJoke_Click ()2: cmdJoke.Caption = &quot;Bird Dogs Fly&quot;3: End Sub</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Throughout this book, you will often see code listings with numbers down the left side. The numbers are used to refer to individual lines. Don't actually type the numbers or the colons that follow them when you enter the program code.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Analysis:</I></B></FONT> No matter what the command button's Caption property is set to at the start of the program, when the user clicks the command button, this event procedure executes and the command button's caption changes to Bird Dogs Fly (line 2).<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>You will see a more comprehensive program that assigns data values to and from form controls in this lesson's project.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Provided that you have added the CONSTANT.TXT file to AUTOLOAD.MAK, you can assign the named constants in CONSTANT.TXT to controls inside event procedures. The following assignment statement adds a single line border around the label named lblSinger:<BR><BR><PRE><FONT COLOR="#000080">lblSinger.BorderStyle = FIXED_SINGLE</FONT></PRE><P>Of course, you must know the names of the named constants inside CONSTANT.TXT before you can assign them. Throughout this book, I will point out when you can use named constants for setting properties.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>When you are not using CONSTANT.TXT, assign only the number when a control's property can accept a limited range of values. For example, the possible values that you can select for a label's BorderStyle in the Properties window are 0-None and 1-Fixed Single. To assign either border style directly without using a named constant, assign just 0 or 1. Don't spell out the entire property. For example,<BR>lblSinger.BorderStyle = 1</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Using the assignment statement, you can assign constants, named constants, control values, and variables to other variables and controls. The assignment statement requires that your variables first be defined with Dim if your program's (general) procedure contains an Option Explicit 1 statement, as I have suggested.<BR><BR><A NAME="E68E58"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Mathematical Expressions</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Data values and controls are not the only kinds of assignments that you can make. With the Visual Basic math operators, you can calculate and assign expression results to variables when you code assignment statements that contain expressions.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>An <I>operator</I> is a word or symbol that does math and data manipulation.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>It is easy to make Visual Basic do your math. Table 7.3 describes Visual Basic's primary math operators. There are other operators, but the ones in Table 7.3 will suffice for most of the programs that you write. Look over the operators. You are already familiar with most of them because they look and act just like their real-world counterparts.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 7.3. The primary math operators.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Operator</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Example</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>+</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Net + Disc</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Adds two values</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>-</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Price - 4.00</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Subtracts one value from another value</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>*</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Total * Fact</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Multiplies two values</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>/</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tax / Adjust</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Divides one value by another value</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>^</FONT><TD VALIGN=top  BGCOLOR=#80FFFF 

⌨️ 快捷键说明

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