Type Cast Operator

Definition

Use the type cast operator to perform explicit type conversions. The type cast operator converts the data type of expr to the type specified by type-name:

[type-name] expr
  • Explicit type conversions require the type cast operator.
  • Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.

The following table summarizes type conversions:

Explicit Type Conversions Implicit Type Conversions

BOOLEAN to INTEGER

INTEGER to enum

BOOLEAN to enum

non-string to STRING

record to LIST

STRING to INTEGER

STRING to GUITYPE

** DATETIME to DATE

** DATETIME to TIME

** DATE to DATETIME

** TIME to DATETIME

SET to LIST

SET to INTEGER

INTEGER to SET

INTEGER to SEMAPHORE

INTEGER to REAL

INTEGER to BOOLEAN

**** REAL to INTEGER

enum to INTEGER

LIST to record

LIST to ARRAY

ARRAY to LIST

* LIST OF type-1 to LIST OF type-2

* ARRAY of type-1 to ARRAY OF type-2

GUITYPE to INTEGER

enum to BOOLEAN

*** STRING to DATETIME

STRING to HMACHINE

LIST to SET

* Applies conversion rules to the built-in types in the list or array

** See DATETIME data type description for more information.

*** If string is ISO format: YYY-MM-DD HH.MM.SS.MSMSMS

**** Casting a REAL to an INTEGER will truncate the REAL number.

Example

[-] main ()
	[ ] CastIntToBOOLEAN() 
	[ ] CastStringToGUITYPE() 
	[ ] CastStringToDATETIME() 
	[ ] CastStringToInt() 
[ ] 
[-] testcase IntToBOOLEAN()
	[ ] Print([BOOLEAN]0 == FALSE) // prints: TRUE 
	[ ] Print([BOOLEAN]1 == TRUE) // prints: TRUE 
	[ ] 
[-] testcase CastStringToGUITYPE()
	[ ] STRING sName = "msw" 
	[ ] Print([GUITYPE]sName == msw) // prints: TRUE 
[ ] 
[-] testcase CastStringToDATETIME()
	[ ] DATETIME GrandOpening = "2006-12-02 09:00"
	[ ] sGrandOpening = FormatDateTime (GrandOpening,"mm/dd/yy") 
	[ ] Print (sGrandOpening) // prints "12/02/06"
[ ] 
[-] testcase CastStringToInt()
	[ ] STRING sName = "SilkTest" 
	[ ] Print([INTEGER]sName == 83) // prints: TRUE
Note: The result of casting a string to an integer is the numeric value of the first character in the string. Thus in the CastStringToInt example above the result is 83, which is the decimal value of the ASCII character S.

SET <___> INTEGER Example

The first 32 members of a SET variable can be implicitly cast as an INTEGER variable. The result is a bit mask with each set bit corresponding to a value in that SET data type that is contained in this SET variable.

[ ] // Establish a COLORS data type with 6 possible colors
[-] type COLORS is set
	[ ] Red 
	[ ] Blue 
	[ ] Yellow 
	[ ] Green 
	[ ] Purple 
	[ ] Orange
	[ ] 
[ ] // Create a COLORS object
[ ] COLORS CoolColors = {Blue, Green, Purple}
[ ] Print ([INTEGER] CoolColors) // prints: 26 (explicit cast)
[ ] Print (CoolColors == 26) // prints: TRUE (implicit cast)

The bit mask representing CoolColors is 0000 0000 0001 1010, which is the hexadecimal value 0x001A, or the decimal value 26.