TypeOf Function

Action

Returns the data type of an expression.

Syntax

type = TypeOf (aExpression)
Variable Description
type The data type of the expression. DATATYPE.
aExpression The expression to evaluate. ANYTYPE.

Notes

TypeOf returns the data type of an expression. A variable must be initialized before TypeOf can evaluate its data type. If you pass TypeOf an uninitialized variable, Silk Test Classic raises the exception E_VAR_NOT_SET.

Example: Printing data types

[-] type CRITTER is enum
	[ ] Beaver 
	[ ] Otter 
	[ ] Cat 
	[ ] Weasel 
[-] testcase TypeOfExample ()
	[ ] BOOLEAN bPrint = TRUE
	[ ] INTEGER iOccurrences = 27
	[ ] NUMBER nTimes = -0.003 
	[ ] REAL rTemperature = -40.09 
	[-] ARRAY[3] OF ANYTYPE aaStuff = {...} 
		[ ] 'Hello' 
		[ ] 14.3 
		[ ] FALSE 
	[ ] CRITTER cAnimal = Cat 
	[ ] Print (TypeOf (bPrint)) // prints: boolean 
	[ ] Print (TypeOf (iOccurrences)) // prints: integer 
	[ ] Print (TypeOf (nTimes)) // prints: real 
	[ ] Print (TypeOf (rTemperature)) // prints: real 
	[ ] Print (TypeOf (aaStuff)) // prints: array[3]of anytype 
	[ ] Print (TypeOf (cAnimal)) // prints: CRITTER

Example: Using TypeOf with a switch statement

[-] void SwitchByType(ANYTYPE value)
     [ ] print(TypeOf(value))
     [-] switch(TypeOf(value))
          [-] case STRING
                [ ] STRING s = value
                [ ] // do things with s...
          [-] case INTEGER
                [ ] INTEGER i = value
                [ ] // do things with i...
          [-] case LIST OF STRING
                [ ] LIST OF STRING ls = value
                [ ] // do things with ls...
[ ] 
[-] testcase SwitchByTypeExample() appstate none
     [ ] SwitchByType("text")
     [ ] SwitchByType(5)
     [ ] SwitchByType({"hello", "world"})