Variables and Data Types |
|
|
Data Types |
Unlike some other languages, VBScript recognizes only a data type called Variant when declaring a variable using the following formula: Dim VariableName The Variant data type can be used in place of any type. After declaring the variable, its type is still not know. If you had requested a value from the user, before involving it in a calculation, you should first convert it to the appropriate type. Fortunately, VBScript provides the necessary means (functions) to perform this conversion. |
String |
A string is an empty space, a character, or a group of characters. When you present a control to the user who is supposed to type in it, the value that the user enters is primarily a string, even if you requested a character or a number. Here is an example of declaring a variable that would hold a string: Dim Country To initialize the string, you can pass a double-quoted value to it. Here is an example: <%@ Language="VBScript" %> <html> <head> <title>Active Server Pages Tutorials</title> </head> <body> <h1>Lesson 4: Variables and Data Types</h1> <p><b>Lecturer:</b> Benjamin Jacobson</b> <br> <b>Time Allocated:</b> 74 minutes</p> <p>This lesson introduces the use of the computer memory to store some values that would be used while a visitor is interacting with a web page.</p> <% Dim Country Country = "Argentina" Response.Write(Country) %> <h3>Enjoy</h3> </body> </html> To convert a value or an expression into a string, you can use the CStr() function. To use it, pass the value or the expression in the parentheses of this function. |
Practical Learning: Using Strings |
<%@ Language="VBScript" %> <html> <head> <title>La Familia: Membership</title> </head> <body> <h1>Membership Application</h1> <% Dim strFirstName Dim strLastName Dim strFullName strFirstName = Request.Form("txtFirstName") strLastName = Request.Form("txtLastName") strFullName = strLastName + ", " + strFirstName Response.Write("<b>New Member:</b> ") Response.Write(strFullName) %> </body> </html> </body> </html> |
<%@ Language="VBScript" %> <html> <head> <title>La Familia: Membership</title> </head> <body> <h1>Membership Application</h1> <form method="POST" action="members.asp"> <table border="0" width="357"> <tr> <td width="194">First Name:</td> <td width="8"><input type="text" name="txtFirstName" size="14"></td> <td width="135"></td> </tr> <tr> <td width="194">Last Name:</td> <td width="8"><input type="text" name="txtLastName" size="14"></td> <td width="135"><input type="submit" value="Submit" name="btnSubmit"></td> </tr> </table> </form> </body> </html> </body> </html> |
Natural Numbers |
A number is referred to as natural when it is considered as a whole. Examples are 2, 408, or 732945793. Active Server Pages supports various types of natural numbers. A byte is a small positive number that is less than or equal to 255. After getting a value or an expression, to convert it to a Byte, you can use the CByte() function. To do this, pass the value or the expression in the parentheses of CByte(). An integer is a natural number whose value can range from -32768 to 2,147,484,647. After getting a value or an expression that represents a natural number, to convert it to a natural number, you can use the CInt() function by entering the value or the expression in the parentheses of this function. If the value is significantly large beyond the range of an integer, to convert it, use the CLng() function and pass the value or the expression in the parentheses of this function. |
Practical Learning: Using Natural Numbers |
<%@ Language="VBScript" %> <html> <head> <title>CD Publisher</title> </head> <body> <h1>CD Publisher</h1> <form method="GET" action="cdpublisher1.asp"> <table border="0" width="398"> <tr> <td width="214">Number of CDs Ordered:</td> <td width="82"><input type="text" name="txtQuantity" size="10" value=<% =Request.QueryString("txtQuantity") %> > </td> <td width="82"><input type="submit" value="Calculate" name="btnCalculate"></td> </tr> <tr> <td>Each CD will cost</td> <td width="76"><input type="text" name="txtUnitPrice" size="10" value=<% =Request.QueryString("txtUnitPrice") %> ></td> <td width="75"></td> </tr> <tr> <td width="214">And the total price is:</td> <td width="82"> <input type="text" name="txtTotalPrice" size="10" value= <% =CInt(Request.QueryString("txtQuantity")) * Request.QueryString("txtUnitPrice") %> > </td> <td width="82"><input type="reset" value="Start New Order" name="btnReset"></td> </tr> </table> </form> </body> </html> |
Decimal Numbers |
A number is referred to as decimal or real when it contains a fractional part. Such a number is considered in two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel: On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers. A Single variable can store real numbers that range from ±1.5 × 10−45 to ±3.4 × 1038 with a precision of 7 digits in 32 bits. Here is an example of declaring a variable that would hold a decimal number: Dim number To initialize a Single variable, you can assign it a natural number or a number that includes the decimal separator. Here is an example: <% Dim number number = 128.68 Response.Write(number) %> To convert a value to Single, you can use the CSng() function. When a variable is larger than the Single can handle and requires more precision, it is referred to as Double. A Double value can store very large numbers ranging from ±5.0 × 10−324 to ±1.7 × 10308 with a precision of 15 or 16 digits. Here is an example of declaring a variable for it: Dim distance To convert a value to Double, you can use the CDbl() function. |
Practical Learning: Using Real Numbers |
<%@ Language="VBScript" %> <html> <head> <title>CD Publisher</title> </head> <body> <h1>CD Publisher</h1> <form method="GET" action="cdpublisher1.asp"> <table border="0" width="398"> <tr> <td width="214">Number of CDs Ordered:</td> <td width="82"><input type="text" name="txtQuantity" size="10" value=<% =Request.QueryString("txtQuantity") %> > </td> <td width="82"><input type="submit" value="Calculate" name="btnCalculate"></td> </tr> <tr> <td>Each CD will cost</td> <td width="76"><input type="text" name="txtUnitPrice" size="10" value=<% =Request.QueryString("txtUnitPrice") %> ></td> <td width="75"></td> </tr> <tr> <td width="214">And the total price is:</td> <td width="82"> <input type="text" name="txtTotalPrice" size="10" value= <% =CInt(Request.QueryString("txtQuantity")) * CDbl(Request.QueryString("txtUnitPrice")) %> > </td> <td width="82"><input type="reset" value="Start New Order" name="btnReset"></td> </tr> </table> </form> </body> </html> |
Date and Time |
Active Server Pages provides support for dates and times values as their own data type. Here is an example of declaring a variable used to hold a date or a time value: Dim StartDate To initialize a date or time variable, include the value between two # signs and assign that value to the variable. To specify a date value, use the formula #M/d/yyyy#. To specify a time value, you can use any variance of #HH:MM AM/PM# Here are three examples: <%@ Language="VBScript" %> <html> <head> <title>Active Server Pages Tutorials</title> </head> <body> <h1>Lesson 4: Variables and Data Types</h1> <p><b>Lecturer:</b> Benjamin Jacobson</b> <br> <b>Time Allocated:</b> 74 minutes</p> <p>This lesson introduces the use of the computer memory to store some values that would be used while a visitor is interacting with a web page.</p> <% Dim StartDate Dim StartTime Dim StartDateAndTime StartDate = #04/22/2004# StartTime = #10:42 AM# StartDateAndTime = #04/22/2004 10:42 AM# Response.Write(StartDate) Response.Write("<br>") Response.Write(StartTime) Response.Write("<br>") Response.Write(StartDateAndTime) %> <h3>Enjoy</h3> </body> </html> This would produce: |
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|