vel07.htm

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

HTM
2,743
字号
</UL></UL><P>Notice the quotation marks around the three string values. string values require the quotation marks, which tell Visual Basic where the string begins and ends. If you wanted to embed spaces at the beginning or end of a string, you indicate those spaces by including them inside the quotation marks. For example, here are three different strings, each with a different set of embedded spaces: &quot; house&quot;, &quot;house &quot;, &quot; house &quot;. You cannot embed quotation marks directly inside a string; Visual Basic thinks the string ends as soon as it comes to the second quotation mark, even if it is not the actual end of the string.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>You just caught me in a lie. Actually, there is a way to embed quotation marks in string data, but doing so is messy and requires the use of a built-in function that you will learn about in Lesson 7.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following data values can take on the variant data type:<BR><UL><UL><P>&quot;^%#@#$%3939$%^&amp;^&amp;&quot;<BR></UL></UL><UL><UL><P>123.45 <BR></UL></UL><UL><UL><P>4.532112E+92<BR></UL></UL><UL><UL><P>21<BR></UL></UL><UL><UL><P>03-Mar-1996<BR></UL></UL><P>Do these values look familiar? The variant data type can hold data of <I>any </I><I>other data type</I>. Think about the kind of data stored in label controls. You often want to display numbers, dollar amounts, times, and dates in labels on a form. You can always store variant data in these controls. The data that comes <I>from</I> these controls is also variant.<BR><P>Visual Basic offers you several ways to convert data from one type to another. For example, &quot;34&quot; is a string value because of the quotation marks. You cannot perform math on string data. However, if you store a string that contains a valid number and decide that you need to perform math on the value, Visual Basic provides a built-in data-conversion routine that enables you to convert the string to a number so that you can do math with its value. The data type differences exist so that you can adequately store and describe certain data values.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic uses the variant data type for date and time values, as you will learn in Lesson 7, &quot;Functions and Dates.&quot;</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>constant</I> is a data value that does not change.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The data values that you saw in this section are all constants. A constant does not change. For example, the number 7 is always 7, and never means anything except 7. There are times when you must explicitly state exactly what kind of constant a data value is so that Visual Basic works properly with the data. For example, 7 is a constant, and neither you nor Visual Basic can tell whether 7 should be stored in an integer or a long integer data location.<BR><P>variant data is the only kind of data that Visual Basic changes, when needed, to suit the situation at hand. In other words, if you store a long integer value in a variant data area, you can perform math with the variant value. If you store a string in a variant data area, you'll be able to perform string operations on the data.<BR><P>If you run into a situation where a particular data type constant is required, you don&#146;t have to use of the default variant data type. You can explicitly state what data type a certain constant is. By adding one of the data type suffix characters listed in Table 7.2 to your constant data, you supply the data type when you use the constant.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 7.2. Data type suffix characters.</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>Suffix</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Data Type</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Example</I></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>14&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>14!</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>14#</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>14@</FONT></TABLE><P>You don&#146;t need the suffix characters very often. Most of the time, you can get by with assigning constants to controls and data areas without needing to add a suffix character to the end of the name. Remember that Visual Basic assumes the data constant is a variant data type unless you use a suffix character to override that assumption. Because variant data can convert to any other data type, you almost always can use the default variant data type.<BR><P>Your programs are made up of the data that changes and constant data, which remains the same. Many times you must specify constant values, such as the number of months in a year, the number of hours in a day, or a company name. Other times, you work with values that change over time, such as an employee's salary or an inventory valuation. The next section discusses how to set up and use data values that change over time.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Is your head spinning yet? If you have never programmed before&#151;and this book assumes that you never have&#151;this talk about data types has jumped right into hard material that seems technical and frustrating. You really haven't seen a reason to use these data types yet. The next section helps explain why these data types are so important. So don't give up just yet&#151;not that you were going to, but I just wanted to be sure!</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>All Visual Basic data falls in one of seven data types. The kind of data you work with and the range of the data values that you use determine which data type a value will take. The various data types enable you to categorize data and to perform specific functions on certain kinds of data.<BR><BR><A NAME="E68E57"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Variable Storage</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>As you know, your computer has memory. You can store data values in memory locations that your Visual Basic program then operates on. When you define variables, you reserve places in memory for data variables, name those variables, and decide what data type those variables will hold.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>variable</I> is a named storage location that holds data that changes.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Throughout a program, you need to store data in memory locations while you compute with that data. Those memory locations are called <I>variables</I>. A variable, unlike a constant, can change. In other words, you can store a number in a variable early in the program and then change that number later in the program. Some variables might not change&#151;such as when the application does not need the variable to change&#151;but often the contents of variables do change.<BR><P>A variable is like a box in memory that holds a data value. The two characteristics of variables are<BR><UL><LI>Every variable has a name.<BR><BR><LI>Every variable can hold only one kind of data.<BR><BR></UL><P>The following sections explain how you can request variables from Visual Basic so that you have places to put data values that you need to store.<BR><BR><A NAME="E69E49"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Defining Variables</B></FONT></CENTER></H4><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>To <I>define</I> a variable means to create and name a variable.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>A program can have as many variables as you need it to have. Before you can use a variable, you must request that Visual Basic create the variable by defining the variable first. When you define a variable, you tell Visual Basic these two things:<BR><UL><LI>The name of the variable<BR><BR><LI>The data type of the variable<BR><BR></UL><P>Once you define a variable, that variable always retains its original data type. Therefore, a single-precision variable can hold only single-precision values. If you stored an integer in a single-precision variable, Visual Basic would convert the integer to a single-precision number before it stored the number in the variable. Such conversions are common, and they typically do not cause many problems.<BR><P>The Dim statement defines variables. Using Dim, you tell Visual Basic<BR><UL><LI>that you need a variable<BR><BR><LI>what to name the variable<BR><BR><LI>what kind of variable you want<BR><BR></UL><P>Dim&#151;short for <I>dimension</I>&#151;is a Visual Basic statement that you write in an application&#146;s Code window. Whenever you learn a new statement, you need to learn the format for that statement. Here is the format of the Dim statement:<BR><BR><PRE><FONT COLOR="#000080">Dim VarName AS DataType</FONT></PRE><P><I>VarName</I> is a name that you supply. When Visual Basic executes the Dim statement at runtime, it creates a variable in memory and assigns it the name you give in the <I>VarName</I> location of the statement. <I>DataType</I> is one of the seven Visual Basic data types that you learned about in Table 7.1.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Think of a variable as an internal label control that the user cannot see. The variable has a name and holds data just like the label control has a Name property and holds a caption. Variables are more specific than controls, however, because of their data type requirements.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Variable names must follow the same naming rules as controls. In <A HREF="vel04.htm">Unit 4</A>, you learned the naming conventions for a control&#146;s Name property&#151;for example, the name must be from 1 to 40 characters long, cannot be a reserved word, and so on.<BR><P>Always use a Dim statement to define variables before you use variables. If the Options Environment Require Variable Definition option is set to Yes&#151;as it is by default&#151;Visual Basic will issue an error message whenever you use a variable that you have not defined. This option prevents you from inadvertently misspelling variable names and helps you avoid errors in your program.<BR><P>Suppose that you give a partial program to another Visual Basic programmer to work on. If you want to require that all variables be defined but are unsure of the other programmer's Options Environment Require Variable Definition setting, select (general) from the Code window's Object dropdown list and add the following special statement:<BR><BR><PRE><FONT COLOR="#000080">Option Explicit</FONT></PRE><P>No matter how the Options Environment setting is set, the programmer cannot slip variables into your program without defining them properly in Dim statements. Again, requiring variable definitions helps eliminate bugs down the road, so you are wise to get in the habit of putting Option Explicit in the (general) section of every program&#146;s Code window.<BR><P>The following statement defines a variable named ProductTotal:<BR><BR><PRE><FONT COLOR="#000080">Dim ProductTotal As Currency</FONT></PRE><P>From the Dim statement, you know that the variable holds currency data and that its name is ProductTotal. All Visual Basic commands and built-in routines require an initial capital letter. Although you don't have to name your variables with an initial capital letter, most Visual Basic programmers do for the sake of consistency. Additional caps help distinguish individual words inside a name. (Remember that you cannot use spaces in the name of variable.)<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The Static and Global statements also define variables, as you will see in Lessons 6 and 8.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following statements define integer, single-precision, and double-precision variables:<BR><PRE><FONT COLOR="#000080">Dim Length As IntegerDim Price As SingleDim Structure As Double</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Except for rare occasions that you will learn about in Lesson 8, &quot;Modular Programming,&quot; never define two variables with the same name. Visual Basic won&#146;t know which one you are referring to when you try to use one of them.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>variable-length string</I> holds strings of any length.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you want to write a program that stores the user's text box entry for the first name, you would define a string like this:<BR><BR><PRE><FONT COLOR="#000080">Dim FirstName As String</FONT></PRE><P>You can get fancy when you define strings. This FirstName string can hold any string from 0 to 65,500 characters long. You will learn in the next section how to store data in a string. FirstName can hold data of virtually any size. You could store a small string in FirstName&#151;such as &quot;Joe&quot;&#151;and then store a longer string in FirstName&#151;such as &quot;Mercedes&quot;. FirstName is a variable-length string.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>fixed-length string</I> holds strings of a preset maximum fixed size.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Sometimes you want to limit the amount of text that a string holds. For example, you might need to define a string variable to hold a name that you read from the disk file. Later, you will display the contents of the string in a label on the form. The form's label has a fixed length, however&#151;assuming that the AutoSize property is set to True. Therefore, you want to keep the string variable to a reasonable length. The following Dim statement demonstrates how you can add the <I>* StringLength</I> option when you want to define fixed-length strings:<BR><BR><PRE><FONT COLOR="#000080">Dim Title As String * 20</FONT></PRE><P>Title is the name of a string variable that can hold a string from 0 to 20 characters long. If the program attempts to store a string value that is longer than 20 characters in Title, Visual Basic truncates the string and stores only the first 20 characters.<BR><P>Here's a shortcut: You can omit the As Variant descriptor when you define variant variables. This Dim statement:<BR><BR><PRE><FONT COLOR="#000080">Dim Value As Variant</FONT></PRE><P>does exactly the same thing as<BR><BR><PRE><FONT COLOR="#000080">Dim Value</FONT></PRE><P>A good rule of thumb is to make your code as explicit as possible. In other words, As Variant requires extra typing at the keyboard, but using the As Variant makes your request for a variant variable clearer. When you later maintain the program, you will know that you <I>meant</I> to define a variant variable and that you did not accidentally omit a different data type.

⌨️ 快捷键说明

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