ActionScript Programming/PartI/Chapter 3/Number,Boolean,Date

Number
In section General concepts and difference between Number and String data types you understood the difference between String and Number data types. Now we will analyze Number data type in details. The following is the structure of declaring Number variable:

var = new Number( [initnumber] );

The parameter is the name of the variable you want to declare, and the parameter [initnumber] is the value you want the variable to be assigned when it is created.

toString
String.toString;

This method returns the string representation of the specified String object. You already know the difference between Number and String objects so there is no need to explain why we need this method.

valueOf
String.valueOf;

This method returns the primitive value type of the specified Number variable (object).

Boolean
Now lets test the Boolean data type. Add a new button or movie clip in the scene and set its’ name to “testobject”, then open the actions of Frame 1 and write the following in Script pane:

The first line declares a Boolean variable “show” and sets its’ value to “false”. In the third line we set “testobject’s” property “Visible” to the “show” variable we declared. If you test the movie you won't see the “testobject”, because the show value is “false” but if you change the word “false” in line 1 to “true” then the object will be shown.

toString
String.toString;

This method returns the string representation of the Boolean object. Now you ask why do you need to use this method if you can do without it by just writing for example: “x=x+true;”, where x is a String variable. To understand the use of “toString” method you must first understand the Boolean data type. Actually the Boolean data type is a masked number data type. The values “true” and “false” are numbers. “true” is 1 and “false” is 0. To test this, we can write a simple code:

If you run the program, you will see “0” in “output window”. Lets understand what we have done. First we declared a Number variable “i”, then assigned “false” to it. If you omit the third line, the output will be “false”. But what have we done? In line three we added 0 to variable “i”. Why have we done this? Because we wanted Flash to think that we added some number to variable “i” which would change the value of it. So it converted the stored “false” value to number and then added 0 to it. Briefly we made Flash convert the value “false” to number. If we write “true” instead of “false” in line 2, then you will see that the traced value is 1. This means that actually “true” and “false” values are numbers and correspondingly equal to 1 and 0.

valueOf
String.valueOf;

This method returns true if the primitive value type of the specified Boolean object is true, false if it is false.

Date
This is a very complicated data type, it has 37 methods. Now we will learn some of them.

The first line declares a new Date variable “d”. So now the date is stored there. In lines 3-11 we trace the values of our “d” variable.

Methods
<< Previous   Next >>