📄 vel13.htm
字号:
<P>Earlier, you learned how to use Str$() to convert a number to a string. Because Str$() always converts positive numbers to strings with a leading blank (where the imaginary plus sign appears), you can combine LTrim$() with Str$() to eliminate the leading blank. The first of the following two statements stores the leading blank in st1. The second uses LTrim$() to get rid of the blank before storing the string into st2.<BR><PRE><FONT COLOR="#000080">st1 = Str$(234) ' Stores " 234"st2 = LTrim$(Str$(234)) ' Stores "234"</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The string functions manipulate strings and return values based on string arguments. As you write more powerful programs, you'll need to extract substrings and convert the case of strings in various ways to display string values on the form and in controls.<BR><BR><A NAME="E68E102"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>General Functions</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are a handful of general functions that don't fall into a strict numeric or string category. These general-purpose functions add power to your programming skills. This section explains how to detect data types using the Is...() and <I>VarType</I>() functions. You may not see a use for these functions quite yet, but familiarize yourself with them. In Lesson 8, you'll learn how to pass data back and forth between Visual Basic routines, and there are times when you'll need to know which data type was passed to you using one of the functions taught here.<BR><P>Table 13.5 contains <I>data inspection</I> functions, often called the <I>Is...()</I> functions. When you store a data value in a variant variable (which can accept any data type), Table 13.5's functions returns a true or false result that indicates whether the argument can be converted to a specific data type.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.5. The </B><B>Is...()</B><B> data inspection 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 Name</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>IsDate()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether its argument can be converted to a valid date</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>IsEmpty()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether its argument has been initialized with any value since the argument's original definition</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>IsNull()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether its argument holds a Null value (such as an empty string)</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>IsNumeric()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether its argument holds a value that can be converted to a valid number</FONT></TABLE><P>The following code ensures sure that a valid string appears in the variable named ans before applying a UCase$() function:<BR><PRE><FONT COLOR="#000080">If IsString(ans) Then NewAns = UCase$(ans)Else MsgBox "You didn't type a letter!"End If</FONT></PRE><P>Empty variables differ from Null values and zero values. Empty variables indicate that nothing has been entered into variables. The IsNull() function checks to see whether its control argument contains a Null value.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Use IsNull() to see whether a control or field on a form contains data. Use IsEmpty() just for variables.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The VarType() function determines the data type of its argument. Table 13.6 lists the return values from the VarType() function. VarType() returns no values other than the nine listed in the table.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.6. The </B><B>VarType()</B><B> function's return values.</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>Return</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>0</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Empty</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>1</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Null</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>2</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Integer</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>3</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Long</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>4</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Single</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>5</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Double</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>6</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Currency</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>7</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is Date</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>8</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates that the argument is String</FONT></TABLE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 13.1 makes sure that the user entered a value into the txtAge text box control.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The general-purpose functions return values that indicate data contents and data types. You can use these functions to see whether variables and controls have been initialized before calculating with those values.<BR><P><FONT COLOR="#000080"><B>Listing 13.1. Checks to make sure that the user entered a value.</B></FONT><BR><PRE><FONT COLOR="#000080">1: If IsNull(txtAge.Text) Then2: MsgBox "You didn't type anything!"3: Else4: MsgBox "Thanks for entering a value."5: End If</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 ensures that the user has typed <I>something</I> in the control named txtAge. Subsequent code could then use the VarType() function to make sure that the user entered a value of the correct data type required by the user.<BR><BR><A NAME="E68E103"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E90"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is a function <I>argument</I>?<BR><BR><LI>True or false: A function's return value must match the argument list's data type.<BR><BR><LI>True or false: Some functions require no arguments, some one, and some several.<BR><BR><LI>True or false: The function's arguments must all match in data type.<BR><BR><LI>Why are there three integer functions?<BR><BR><LI>What value does Visual Basic store in Ans given the following assignment statements?<BR>Ans = Int(61.32)<BR>Ans = Int(-61.32)<BR>Ans = Fix(-61.32)<BR>Ans = Cint(421.51)<BR><BR><LI>True or false: Int(), CInt(), and Fix() all return the same value for positive arguments.<BR><BR><LI>What function performs the opposite of Chr$()?<BR><BR><LI>What value appears in the string variable named AList after these assignments?<BR>AList = UCase$("AbCdEfG")<BR>AList = LCase$("AbCdEfG")<BR><BR><LI>What would the following assignment statement store in the variant variable named Anything?<BR>Anything = Str$(Val("78.1"))<BR><BR><LI>Write the values stored in each of the following assignment statements:<BR>AStr = Left$("Sams", 1)<BR>AStr = Left$("Sams", 3)<BR>AStr = Right$("Sams", 1)<BR>AStr = Right$("Sams", 3)<BR>AStr = Mid$("Sams", 2, 3)<BR><BR><LI>True or false: Depending on the arguments, Mid$() could return the same values as Left$() and Right$().<BR><BR><LI>True or false: Both of the following produce the same value when used inside an expression:<BR>Chr$(67)<BR>Asc("C")<BR><BR><LI>What is the name of the function that converts a number to a string?<BR><BR></OL><BR><A NAME="E69E91"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Suppose that you needed to display a numeric variable's value inside the prompt string of an input box. Describe how you would you concatenate such a variable to the prompt string?<BR><BR><LI>How could you find out the amount of memory consumed by 250 double-precision variables?<BR><BR><LI>Write the code that uses an input box to get a number from the user and then uses a message box to display the square root of that number.<BR><BR><LI>Write the code that uses two separate input boxes to ask the user for a first and last name, and then display in a message box the total number of letters in both names.<BR><BR><LI>Write an assignment statement that stores the ASCII value of "P" in a variable named ValAsc.<BR><BR></OL><BR><A NAME="E69E92"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Rudy gets an overflow message when attempting the following assignment into the integer variable named Weight:<BR>Weight = CInt(32768 * Num)<BR>Describe the problem for Rudy.<BR><BR></OL><BR><A NAME="E69E93"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><OL><LI>Add to Listing 13.1 to make sure that the user entered an integer in the txtAge.Text control.<BR><BR></OL><P>Write a program that stores the 256 ASCII characters (from ASCII 0 to ASCII 255) in a string array that's defined to hold 256 elements.<BR><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="#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="vel14.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 + -