vel07.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,743 行 · 第 1/4 页
HTM
2,743 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel07.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel08.htm"><LINK REL="Previous" HREF="velp03.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel07.htm</H2><P ALIGN=LEFT><A HREF="velp03.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="vel08.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="#E68E55" >What You'll Learn</A><LI><A HREF="#E68E56" >Data Types</A><LI><A HREF="#E68E57" >Variable Storage</A><UL><LI><A HREF="#E69E49" >Defining Variables</A><LI><A HREF="#E69E50" >Assigning Values to Variables</A></UL><LI><A HREF="#E68E58" >Mathematical Expressions</A><LI><A HREF="#E68E59" >The Val() Function</A><LI><A HREF="#E68E60" >Homework</A><UL><LI><A HREF="#E69E51" >General Knowledge</A><LI><A HREF="#E69E52" >Write Code That...</A><LI><A HREF="#E69E53" >Find the Bug</A><LI><A HREF="#E69E54" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E10"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 4, Unit 7</B></FONT></CENTER></H1><BR><A NAME="E67E13"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Variables, Controls, and Math</B></FONT></CENTER></H2><BR><BR><A NAME="E68E55"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Data Types<BR><BR><LI>Storing variables<BR><BR><LI>Mathematical expressions<BR><BR><LI>The Val() function<BR><BR></UL><P>It's time to learn a foreign language: Visual Basic. Until now, you created programs by placing controls on the form and setting property values for them. Visual programming like that is at the heart of Visual Basic, and visual nature of Visual Basic is what separates it from the more traditional text-based programming languages.<BR><P>As you will see in this lesson, there is more to Visual Basic than just a pretty face! If you learn the text-based Visual Basic language in addition to how to place and interact with controls, you can write extremely powerful applications. The Visual Basic programming language—the code—often ties controls together so that your applications can analyze data and compute based on your data.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Visual Basic is one foreign language that is really not so foreign. You will see that its vocabulary consists of simple words that you are already familiar with.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E56"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Data Types</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Visual Basic language works with all kinds of data. Before you learn how to manipulate data, you will learn how to distinguish among the various data types.<BR><P>Visual Basic manipulates and analyzes seven kinds of data. Table 7.1 describes the data types that Visual Basic supports. Some data falls into more than one category. For example, a dollar amount can be considered both a currency data type and a single data type. When you write a program, you need to decide which data type best fits your program's data values.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 7.1. Visual Basic data types.</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>Data Type</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>integer</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Numeric values with no decimal point or fraction. integer values range from –32,768 to 32,767.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>long</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Integer values with a range beyond than that of integer data values. long data values range from –2,147,483,648 to 2,147,483,647. long data values consume more memory storage than integer values, and they are less efficient. The long data type is often called long integer.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>single</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Numeric values that range from –3.402823E+38 to 3.402823E+38. The single data type is often called single-precision.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>double</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>single numeric values that range from –1.79769313486232E+308 to 1.79769313486232E+308. The double data type is often called double-precision.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>currency</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Data that holds dollar amounts from –$922,337,203,685,477.5808 to $922,337,203,685,477.5807.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>string</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Data that consists of 0–65,500 characters of alphanumeric data. Alphanumeric means that the data can be both alphabetic and numeric. string data values may also contain special characters such as ^%@.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>variant</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Used for data stored in controls and for date and time values.</FONT></TABLE><P>The following data values can take on the integer data type:<BR><UL><UL><P>21<BR></UL></UL><UL><UL><P>0<BR></UL></UL><UL><UL><P>-9455<BR></UL></UL><UL><UL><P>32766<BR></UL></UL><P>You can also store these integer data types as long data types, although doing so wastes storage and time unless you plan to change the values later to extremely large or small integer numbers that require the long data type.<BR><P>The following data values must be stored in a long data type:<BR><UL><UL><P>32768<BR></UL></UL><UL><UL><P>-95445<BR></UL></UL><UL><UL><P>492848559<BR></UL></UL><P>The following data values can take on the single data type:<BR><UL><UL><P>0.01<BR></UL></UL><UL><UL><P>565.32<BR></UL></UL><UL><UL><P>-192.3424<BR></UL></UL><UL><UL><P>9543.5645<BR></UL></UL><UL><UL><P>6.5440E-24<BR></UL></UL><P>Of course, you can store these data values in double storage locations as well. Use double data types only when you need to store extra large or small values.<BR><P>The following data values take on the double data type:<BR><UL><UL><P>-5968.5765934211133<BR></UL></UL><UL><UL><P>4.532112E+92<BR></UL></UL><UL><UL><P>928374.344838273567899990<BR></UL></UL><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>What's that E Doing in the Numbers?: The E stands for <I>exponent</I>. Years ago, mathematicians grew weary of writing long numbers. They developed a shortcut notation called <I>scientific notation</I>. Visual Basic supports scientific notation. You can use scientific notation for single and double numeric data values. When you use scientific notation, you don't have to write—or type, in the case of program writing—extremely long numbers.<BR>When a single or double numeric value contains the letter E, followed by a number, that number is written in scientific notation. To convert a number written in scientific notation to its non-abbreviated form, multiply the number to the left of E by 10 raised to the power indicated by the number to the right of E. Therefore, 2.9876E+17 means to multiply 2.9876 by 10 raised to the 17th power, or 10<SUP>17</SUP>. 10<SUP>17</SUP> is a very large number—it is 10 followed by 16 zeroes. The bottom line is that 2.9876E+17 means the same as 29876 followed by 13 zeros.<BR>If a number uses a negative exponent, you multiply the number to left of E by 10 raised to negative power. Therefore, 2.9876E-17 means the same as 2.9876 multiplied by 10<SUP>-17</SUP>—or 2.9876 divided by 10<SUP>17</SUP>—which turns out to be a small number indeed.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Don’t consider yourself a mathematician? That's okay—Visual Basic calculates all your math for you when you learn how to write programs with this book. Typically, only scientific and engineering programmers need to use scientific notation, and they feel right at home using it. Table 7.1 used scientific notation because there is no practical way of displaying the low or high range for double data values with 307 zeros in a the number.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following data values can take on the currency data type:<BR><UL><UL><P>123.45<BR></UL></UL><UL><UL><P>0.69<BR></UL></UL><UL><UL><P>63456.75<BR></UL></UL><UL><UL><P>-1924.57<BR></UL></UL><P>The currency data type can accept and track numeric values to four decimal places. However, you typically store dollar and cent values in the currency storage locations, and these kinds of values require only two decimal places.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic uses the Windows International settings—found in the Control Panel if you want to take the time to look—to track values such as dates, times, and currency. Therefore, if you have Windows set up for a country, such as France, that uses a comma instead of the decimal point as it is used in America, Visual Basic will support the comma when it displays your values.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Never use a currency data value along with a dollar sign. In other words, the following cannot be a currency value even though it looks like one:<BR><UL><UL><P>$5,234.56<BR></UL></UL><P>Visual Basic does not want you to use a dollar sign or commas in numeric data of any kind—unless, of course, your country uses the comma for the fractional portion of numbers. If your data contains anything other than numbers, a plus sign, a minus sign, or an exponent, Visual Basic cannot treat the data as if it were numeric; therefore, it cannot perform mathematical calculations with the data. Instead, Visual Basic treats the data as string data.<BR><P>The following data values take on the string data type:<BR><UL><UL><P>"London Bridge"<BR></UL></UL><UL><UL><P>"1932 Sycamore Street"<BR></UL></UL><UL><UL><P>"^%#@#$%3939$%^&^&"<BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?