Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Data and Data Types > About data types > About assigning data types and strict data typing | |||
You use variables in Flash to hold values in your code. You can explicitly declare the object type of a variable when you create the variable, which is called strict data typing.
If you do not explicitly define an item as holding either a number, a string, or another data type, at runtime Flash Player will try to determine the data type of an item when it is assigned. If you assign a value to a variable, as shown in the following example, Flash Player evaluates at runtime the element on the right side of the operator and determines that it is of the Number data type:
var x = 3;
Because x was not declared using strict data typing, the compiler cannot determine the type; to the compiler, the variable x can have a value of any type. (See Assigning a data type.) A later assignment might change the type of x; for example, the statement x = "hello" changes the type of x to String.
ActionScript always converts primitive data types (such as Boolean, Number, String, null, or undefined) automatically when an expression requires the conversion and the variables aren't strictly typed.
Strict data typing offers several benefits at compile time. Declaring data types (strict data typing) can help prevent or diagnose errors in your code at compile time. To declare a variable using strict data typing, use the following format:
var variableName:datatype;
|
NOTE |
|
Strict data typing is sometimes called strong typing a variable. |
Because data type mismatches trigger compiler errors, strict data typing helps you find bugs in your code at compile time and prevents you from assigning the wrong type of data to an existing variable. During authoring, strict data typing activates code hinting in the ActionScript editor (but you should still use instance name suffixes for visual elements).
Using strict data typing helps ensure that you don't inadvertently assign an incorrect type of value to a variable. Flash checks for typing mismatch errors at compile time, and displays an error message if you use the wrong type of value. Therefore, using strict typing also helps to ensure that you do not attempt to access properties or methods that are not part of an object's type. Strict data typing means the ActionScript editor automatically shows code hints for objects.
For more information on creating variables, see About variables. For information on naming variables, see About naming variables. For more information on assigning data types, and the types you can assign, see Assigning a data type.
For a sample of the datatypes.fla file, which shows you how to use data types in an application, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/DataTypes folder to access the sample.
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/00000650.html