Page

4.4.0- Conversions

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 04. Variables.

Not tagged.
<< 4.3.5- Concatenating VariablesChapter44.5.0- Constants >>

Conversions

As we now know, when you assign a value to a variant, a subtype is also automatically assigned. Sometimes, however, the assigned subtype is not the one that you intend, and in such cases, a little force is required to explicitly convert the value into the type you actually wanted. VBScript provides plenty of functions to do this for you. In fact, it provides so many of these functions that you'll probably never use many of them. In a moment, we'll exercise the most common of these functions in an example. First, here's a complete list of them:

 

Function

Description

Abs

Returns the absolute value of number.

Asc, AscB, AscW

Returns the ANSI character code of the first letter in a string. AscB is used on byte data while AscW is used on 32-bit platforms that use UNICODE data (a format that falls outside the scope of this book).

Chr, ChrB, ChrW

This is the opposite of Asc, and returns the character of a specific character code. ChrB is used on byte data contained in a string while ChrW is used on 32-bit platforms that use UNICODE data.

CBool

Returns an expression that has been converted into a variant with the subtype Boolean.

CByte

Returns the variable that has been converted into a variant with the
subtype Byte.

CCur

Returns the variable that has been converted into a variant with the
subtype Currency.

CDate

Returns the variable that has been converted into a variant with the
subtype Date.

CDbl

Returns the variable that has been converted into a variant with the
subtype Double.

CInt

Returns the variable that has been converted into a variant with the subtype Integer and rounds it to the nearest whole number. When rounding a number like 0.5, 1.5, 2.5 (where the rounding could be up or down), Cint rounds to the nearest even number.

CLng

Returns the variable that has been converted into a variant with the
subtype Long.

CSng

Returns the variable that has been converted into a variant with the
subtype Single.

CStr

Returns the variable that has been converted into a variant with the
subtype String.

DateSerial

Returns the variable that has been converted into a variant with the subtype Date for given year, month, day. e.g. DateSerial (1999, 11, 1) would return 1st November 1999

 

Function

Description

DateValue

Returns a value representing a variant of subtype Date.

TimeSerial

Returns the variable that has been converted into a variant with the subtype Date for given hour, minute, second.

TimeValue

Returns a variant of subtype Date, representing the specified time. (Note that the VBScript Date subtype can represent both date and time values.)

Hex

Returns a string containing the hexadecimal value of a number.

Oct

Returns a string containing the octal value of a number.

Fix

Returns the integer portion of a number using truncation. e.g. 7.2 becomes 7 as does 7.8.

Int

Also returns the integer portion of a number using truncation. e.g. 7.2 becomes 7 as does 7.8.

Sgn

Returns the sign of a number, i.e. positive or negative.

If you assign a fraction or decimal value to an integer, normally the integer is rounded up or down to the next closest whole number. This process is known as an implicit conversion.

These functions all work in much the same way. As a demonstration, we'll have a look at how to use a couple of them now.

Try It Out – Converting a Variant

In this example we're going to take the value of pi=3.142, read it in as a string, convert to a single data type, convert it into an integer and then finally convert it back to a string. We're also going to display the value and subtype of each variant after the conversion.

 

1.    Type the following program into your favorite editor:

<%Option Explicit%>

<HTML>

<HEAD>

<TITLE>Converting Variants</TITLE>

</HEAD>

<BODY>

 

<%

  Dim strPi, dblPi, intPi,strPi2

  Dim varWhatIsPi1, varWhatIsPi2, varWhatIsPi3, varWhatIsPi4

 

  strPi = "3.142"

  varWhatIsPi1 = TypeName(strPi)

  dblPi = CDbl(strPi)

  varWhatIsPi2 = TypeName(dblPi)

  intPi = CInt(dblPi)

  varWhatIsPi3 = TypeName(intPi)

  strPi2 = CStr(intPi)

  varWhatIsPi4 = TypeName(strPi2)

%>

<P><B>Pi is a <%= varWhatIsPi1 %> and Pi returns <%= strPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi2 %> and Pi returns <%= dblPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi3 %> and Pi returns <%= intPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi4 %> and Pi returns <%= strPi2 %> </B></P>

</BODY>

</HTML>

 

2.    Save it as convert.asp.

3.    Run the page on your preferred browser.

Chapter4_image004

How It Works

This program explicitly declares all of the variants we will use and then assigns the variant strPi a value of "3.142", which is a string. It then reads the subtype of this variant into another variant, varWhatIsPi1:

 

<%

  Dim strPi, dblPi, intPi,strPi2

  Dim varWhatIsPi1, varWhatIsPi2, varWhatIsPi3, varWhatIsPi4

  strPi = "3.142"

  varWhatIsPi1 = TypeName(strPi)

 

Next, we declare three more variants; the value assigned to each is generated by converting the subtype of the preceding one. Hence, the value assigned to dblPi is the conversion of strPi from subtype string to subtype double. In addition, the TypeName of the variant dblPi is assigned to varWhatIsPi2:

 

  dblPi = CDbl(strPi)

  varWhatIsPi2 = TypeName(dblPi)

 

The value assigned to intPi is the conversion of dblPi from subtype double to subtype integer. The TypeName of intPi is assigned to varWhatIsPi3:

 

  intPi = CInt(dblPi)

  varWhatIsPi3 = TypeName(intPi)

 

Last, the value assigned to strPi2 is the conversion of intPi back to subtype string. The TypeName of intPi is assigned to varWhatIsPi4:

 

  strPi2 = CStr(intPi)

  varWhatIsPi4 = TypeName(strPi2)

%>

 

The final lines simply display the subtype and value of each of these variants:

 

<P><B>Pi is a <%= varWhatIsPi1 %> and Pi returns <%= strPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi2 %> and Pi returns <%= dblPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi3 %> and Pi returns <%= intPi %> </B></P>

<P><B>Pi is a <%= varWhatIsPi4 %> and Pi returns <%= strPi2 %> </B></P>

 

Notice that because of the conversion from subtype double to subtype integer, the fractional part of the value of pi is lost. Further, when you convert back to subtype string, you don't regain the information! (This is because the final conversion is from type integer to subtype string: the value to be converted is 3, not 3.142.) It's easy to lose information in this way, so you should be sure to control your conversions carefully. Don't be put off, though: data conversions are a useful tool to have, and they're not difficult to use.

<< 4.3.5- Concatenating VariablesChapter44.5.0- Constants >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |