
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
46
|
Chapter 2: Variables
conversion cannot be performed). By comparison, VB.NET allows the programmer
to decide whether the compiler should enforce strict typing. Strict typing may seem
cumbersome, but it can prevent the errors that unsuspecting programmers may
encounter due to automatic and dynamic typing, which we’ll consider in the follow-
ing sections.
Automatic Value Conversion
In some contexts, ActionScript expects a specific type of data. If we use a variable
whose value does not match the expected type, the interpreter attempts to convert
the data to the necessary type. For example, if we use a text variable where a number
is needed, the interpreter will try to convert the variable’s text value to a numeric
value for the sake of the current operation. In Example 2-2,
z is set to 2. Why?
Because the subtraction operator expects a number, the value of y is converted from
the string “4” to the number 4, which is subtracted from 6 (the value of
x), yielding
the result 2.
Conversely, if we use a numeric variable where a string is expected, the interpreter
attempts to convert the number to a string. In Example 2-3,
z is set to the string
“64”, not the number 10. Why? Because the second operand in the expression x + y
is a string. Therefore, the
+ operator performs string concatenation instead ...