: type operator

[ modifiers ] var variableName : type
function functionName () : type { ... }
function functionName ( parameter1:type , ... , parameterN:type ) [ :type ]{ ... } 

Used for strict data typing; this operator specifies the variable type, function return type, or function parameter type. When used in a variable declaration or assignment, this operator specifies the variable's type; when used in a function declaration or definition, this operator specifies the function's return type; when used with a function parameter in a function definition, this operator specifies the variable type expected for that parameter.

Types are a compile-time-only feature. All types are checked at compile time, and errors are generated when there is a mismatch. Mismatches can occur during assignment operations, function calls, and class member dereferencing using the dot (.) operator. To avoid type mismatch errors, use strict data typing.

Types that you can use include all native object types, classes and interfaces that you define, and Function and Void. The recognized native types are Boolean, Number, and String. All built-in classes are also supported as native types.

Availability: ActionScript 1.0; Flash Player 6

Operands

variableName : Object - An identifier for a variable. type A native data type, class name that you have defined, or interface name. functionName An identifier for a function. parameter An identifier for a function parameter.

Example

Usage 1: The following example declares a public variable named userName whose type is String and assigns an empty string to it:

var userName:String = ""; 

Usage 2: The following example shows how to specify a function's parameter type by defining a function named randomInt() that takes a parameter named integer of type Number:

function randomInt(integer:Number):Number { 
 return Math.round(Math.random()*integer); 
} 
trace(randomInt(8)); 

Usage 3: The following example defines a function named squareRoot() that takes a parameter named val of the Number type and returns the square root of val, also a Number type:

function squareRoot(val:Number):Number { 
 return Math.sqrt(val); 
} 
trace(squareRoot(121)); 

See also

var statement, function statement


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001307.html