Learning How to Use Strict Data Typing


Strict data typing means that you explicitly tell Flash what data type a variable can hold when you create it or what kind of an object an item is if it's constructed from built-in or custom classes. If you create (or declare) a new variable and strict type it to a particular class (data type), the rules of that class apply. Flash expects only that kind of data to be stored in that variable, such as the String class expecting a string to be contained in the variable. You use strict type to protect yourself from your own mistakes; if you accidentally try to send number information into something that can only hold characters, you'll get an error message long before you try to deploy the application. You have to fix things before you can make them work. For example, if you declare a new variable as in the following code, you are telling Flash to create a new variable called myString that stores only characters:

var myString:String;

You only need to tell Flash what data type to use when you declare the variable; meaning that you only need to strict type initially for each variable you declare. If you try storing a different data type in the variable created with strict typing, such as in the following code, an error occurs when you publish the document:

var myString:String; myString = 15;

An error occurs because you are trying to store a number to that variable, which is not accepted by Flash because you told Flash that you'd only be using that variable for characters. Kind of like trying to convince the guy you rented a garage from (which stores cars) that the alligator you're trying to house is okay. In the above example, Flash is expecting a string of characters, not numbers. Therefore, to fix it, the following code would work:

myString = "15";

The number 15 is now the characters 1 and 5, so you don't throw an error.




Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net