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

📄 vel13.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel13.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel14.htm"><LINK REL="Previous" HREF="velp06.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel13.htm</H2><P ALIGN=LEFT><A HREF="velp06.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="vel14.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="#E68E98" >What You'll Learn</A><LI><A HREF="#E68E99" >A Function Review</A><LI><A HREF="#E68E100" >The Numeric Functions</A><UL><LI><A HREF="#E69E89" >Converting Numbers</A></UL><LI><A HREF="#E68E101" >The String Functions</A><LI><A HREF="#E68E102" >General Functions</A><LI><A HREF="#E68E103" >Homework</A><UL><LI><A HREF="#E69E90" >General Knowledge</A><LI><A HREF="#E69E91" >Write Code That...</A><LI><A HREF="#E69E92" >Find the Bug</A><LI><A HREF="#E69E93" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E19"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 7, Unit 13</B></FONT></CENTER></H1><BR><A NAME="E67E22"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>The Built-In Functions</B></FONT></CENTER></H2><BR><BR><A NAME="E68E98"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>A function review<BR><BR><LI>The numeric functions<BR><BR><LI>The string functions<BR><BR><LI>General functions<BR><BR></UL><P>If you want to build a house, you'll probably buy prefabricated windows and pre-hung doors instead of framing and hanging them yourself. Unless you want to spend extra time doing the work that you don't have to do, using the prefabricated windows and pre-hung doors makes quick work of those housing elements and allows you to concentrate your energies on the more esthetic items (such as talking your spouse into allowing you to partition two-thirds of the master bedroom for a great computer work area...).<BR><P>Visual Basic's built-in functions save you all kinds of programming time just as the pre-built housing parts do when home building. The built-in functions work like miniature programs that perform common tasks so that you can concentrate your efforts elsewhere. If, for instance, you need to round a single-precision value to an integer, Visual Basic supplies not one but <I>three</I> built-in functions that round numbers to integer values.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>You've already seen several built-in functions in this book, such as Val() and Chr$(). Usually, though not always, a function that contains a dollar sign after its name is a <I>string function</I>, and a function without a dollar sign in its name is a <I>numeric function</I>. This unit teaches you about both numeric and string functions.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E99"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>A Function Review</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Functions accept one or more <I>arguments</I> and do work with those arguments. The function then returns a single value.<BR><P>When you use a function inside a Visual Basic statement, it is said that you're <I>calling</I> a function. For example, the following statement <I>calls</I> the Val() function:<BR><BR><PRE><FONT COLOR="#000080">Num = Val(txtAmt.Text) ' Convert entry to number</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>An <I>argument</I> is a value that you pass to a function.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The parentheses after a function name hold one or more arguments. In the preceding statement, txtAmt.Text is the argument. Some functions, such as Val(), accept only one argument. Some functions accept more than one argument. Some functions don't require any arguments, and for those functions, the parentheses are optional. The arguments inside the parentheses are often called the <I>argument list</I>. A function can require none, one, or several arguments.<BR><P>When you specify function arguments, you are <I>passing</I> the arguments to the function. The function works on the argument values that you pass to the function and produces some sort of value from the arguments. As Figure 13.1 illustrates, you send a function one or more arguments and the function, after using the arguments in the argument list, is said to return a value. That value is the answer to the function, more commonly called the <I>return value</I>. Functions are said to <I>receive arguments </I>and <I>return a value</I>.<BR><P><B> <A HREF="13vel01.gif">Figure 13.1. A function works on its arguments and returns one value.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>It's worth making this very clear: A function always returns a single value and <I>never more</I>. Although a function's argument list can sometimes contain five or more arguments, a function never returns more than one value.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If a function requires more than one argument, you'll separate those arguments by one or more commas. For example, here is a function named Right$() that requires two arguments, MyName and 10, in its argument list:<BR><BR><PRE><FONT COLOR="#000080">Last = Right$(MyName, 10) ' Strip off last name</FONT></PRE><P>You'll always have to do something with a function's return value. Here are some things that you might do with a return value:<BR><UL><LI>Display the return value in a control on the form by assigning the function's return value to a control<BR><BR><LI>Assign the return value to a variable<BR><BR><LI>Use the return value inside a calculation<BR><BR><LI>Use the return value as an argument to another function by nesting one function inside the argument list of another function<BR><BR></UL><P>Basically, anything that you might do with a variable or a constant, you'll do with a function's return value. For example, you learned in <A HREF="vel07.htm">Unit 7</A> how Val() works. Val() converts its string (or variant) argument to a number. The converted number is Val()'s return value. Val() will convert its argument to a single, return value; therefore, you can't place Val() on a line by itself like this:<BR><BR><PRE><FONT COLOR="#000080">Val(UserAddress) ' Invalid!</FONT></PRE><P>Val() will accept the string named UserAddress and convert that string to a number. You must make sure that the program does something with that number! The function is just a predefined, built-in routine, supplied for you by Visual Basic, which operates on the argument list and returns a value that you've got to do something with. Here are four uses of Val() that use the return value in different ways:<BR><BR><PRE><FONT COLOR="#000080">lblHouseNum.Caption = Val(UserAddress) ' To a label<BR>MyHouseNum = Val(UserAddress) ' To a variable<BR>OldAge = 65 + Val(txtAge.Text) ' A calculation<BR>Last = Right$(UserName, Val(Ans)) ' Another function</FONT></PRE><P>An argument might even be an expression. The following Val() function first concatenates the string expression used as the argument, and then returns the numerically converted value of that string argument:<BR><BR><PRE><FONT COLOR="#000080">Number = Val(aStr1 &amp; aStr2)</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The argument list's data type doesn't always match the function's return value data type. For example, a function might take a string argument and return a number. A function might require both a string and a numeric argument, and return a string.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The built-in functions never change their arguments. The built-in functions use their arguments to create a new value: the return value.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>There are several built-in functions in Visual Basic that your programs can call. Programs that call functions must do something with the return values from those functions. A function might require one argument or more. If you send an argument list to a function, that function operates on those arguments and returns a single value based on the argument list.<BR><BR><A NAME="E68E100"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Numeric Functions</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Several numeric functions work to save you programming time when you're working with numbers. There are conversion functions, scientific functions, and trigonometric functions.<BR><P>As you read through the following pages, don't worry about memorizing every function. Try to get a general idea of the kinds of functions supplied by Visual Basic. The functions work to save you time. If you need to convert a number or compute a standard formula, the chances are great that Visual Basic includes a function that does the work for you. For example, there is no reason to write an advanced trigonometric sine function, because Microsoft already wrote one for you.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>You don't have to be a math lover to use and appreciate the Visual Basic numeric functions. Actually, the less you like math, the <I>more</I> you'll appreciate the fact that Visual Basic supplies all these functions for you so that you never have to write the code to accomplish the same thing.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E89"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Converting Numbers</B></FONT></CENTER></H4><BR><P>Visual Basic supplies three functions that convert single-precision and double-precision numbers to integer values. When writing applications, you might need to round numbers down or up to their nearest integers. Table 13.1 contains the three integer conversion functions.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.1. The integer conversion functions.</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>Function</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>CInt()</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Rounds fractional values of .5 and more to the next highest integer</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Fix()</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Truncates the fractional portion</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Int()</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Rounds the number down to the integer less than or equal to its arguments</FONT></TABLE><P>Both Fix() and Int() treat positive integers the same way. Both of the following statements return and store 14:<BR><PRE><FONT COLOR="#000080">ans1 = Int(14.6)ans2 = Fix(14.6)</FONT></PRE><P>The Fix() and Int() functions behave differently for negative numbers. The remarks to the right of the following statements indicate the functions' return values:<BR><PRE><FONT COLOR="#000080">ans3 = Int(-14.6) ' Stores -15ans4 = Fix(-14.6) ' Stores -14</FONT></PRE><P>The negative number less than or equal to -14.6 is -15; hence, the return values for Int() shown in the first statement's remark.<BR><P>The CInt() function returns truly rounded numbers, as shown here:<BR><PRE><FONT COLOR="#000080">ans5 = CInt(14.1) ' Stores 14ans6 = CInt(14.6) ' Stores 15ans7 = CInt(-14.1) ' Stores -14ans8 = CInt(-14.8) ' Stores -15</FONT></PRE><P>There are functions similar to CInt() that convert arguments of any data type to any other data type. Table 13.2 contains the rest of the data conversion functions.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.2. The data type conversion functions.</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>Function</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>CCur()</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent currency data type</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>CDbl()</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent double-precision data type.</FONT><TR><TD 

⌨️ 快捷键说明

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