18.01.2013 Views

VBScript Reference Manual for InduSoft Web Studio

VBScript Reference Manual for InduSoft Web Studio

VBScript Reference Manual for InduSoft Web Studio

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>InduSoft</strong> <strong>Web</strong> <strong>Studio</strong> <strong>VBScript</strong> <strong>Reference</strong> <strong>Manual</strong><br />

Data Subtype Conversion<br />

<strong>VBScript</strong> provides several functions that convert a <strong>VBScript</strong> variable from one data subtype to another.<br />

Since <strong>VBScript</strong> uses the Variant data type, these functions are not generally required. However, when<br />

passing data between IWS (or CEView) and VBScipt, or calling built-in IWS functions from <strong>VBScript</strong><br />

where variables need to be put into the proper argument <strong>for</strong>mat, these <strong>VBScript</strong> data subtype<br />

conversion functions can be very useful.<br />

Data Subtype Conversion Functions<br />

Function Description<br />

CBool() Converts an expression to a variant of subtype Boolean<br />

CByte() Converts an expression to a variant of subtype Byte<br />

CCur() Converts an expression to a variant of subtype Currency<br />

CDate() Converts a valid date and time expression to the variant of subtype Date<br />

CDbl() Converts an expression to a variant of subtype Double<br />

CInt() Converts an expression to a variant of subtype Integer<br />

CLng() Converts an expression to a variant of subtype Long<br />

CSng() Converts an expression to a variant of subtype Single<br />

CStr() Converts an expression to a variant of subtype String<br />

Example:<br />

a = -5.2<br />

b = 4<br />

c = “A”<br />

LState = True<br />

StartDate = #4/6/2005#<br />

StartTime = #10:05:20#<br />

d = CByte(a) ‘ Will generate overflow error, Bytes are only positive<br />

d = CByte(d) ‘ d will equal 5 (Byte)<br />

d = CStr(a + b) ‘ d will be “-1.2” (string)<br />

d = CDate(StartDate + 20) ‘ d will be 4/26/2005 (date)<br />

d = CDate(StartTime) ‘ d will be 10:05:20 am (date/time)<br />

d = CDate(StartTime + 20) ‘ d will be 1/19/1900 10:05:20 am (date/time)<br />

d = CDate(StartTime + #1:5#) ‘ d will be 11:10:20 am<br />

d = CInt(LState) ‘ d will be -1<br />

Since <strong>VBScript</strong> does not use explicit data typing, one might expect that you would not get a type<br />

mismatch error. This however, is not necessarily true. For example, if you attempted to sum a number<br />

and a string, you will get a type mismatch error. Users are not allowed to freely mix heterogeneous data<br />

even if all data is of type Variant. Again, type Variant allows the variable and constant data type to be<br />

determined at runtime, instead of being explicitly predefined.<br />

More detail on the Data Subtype Conversion Functions is provided in the <strong>VBScript</strong> Functions section.<br />

Key Notes:<br />

• You can’t pass an alphanumeric string to a conversion function that returns a number<br />

(e.g. CInt() or CLng() functions) if the string has more than one character containing an<br />

ASCII number. If you try to do this, a type mismatch error will occur<br />

• The CStr() function provides the greatest flexibility when converting an expression into<br />

a String data subtype. If you use the CStr() function with a Boolean expression, the<br />

result will be a string of either “True” or “False”. If you use the CStr() function with a<br />

date expression, the date string will follow the operating systems short date <strong>for</strong>mat.<br />

• To convert a string into a date data subtype, you can use either the CDate() function, or<br />

simply assign a date value to a variable, enclosing the value in hashes (#) as follows:<br />

MyDate = “#3/22/2006#”<br />

<strong>InduSoft</strong>, Ltd. 79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!