Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 8 the constants

This is a discussion on Tutorial 8 the constants within the Object Oriented Programming - tutorials forums, part of the Tutorials category; After having seen the properties (variables inside an Actionscript 3.0 class), we can now introduce the constants. The constants ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Object Oriented Programming - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 30-10-07, 06:47
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 8 the constants

After having seen the properties (variables inside an Actionscript 3.0 class), we can now introduce the constants.
The constants are a new characteristic implemented in the version 3.0. In fact, with the 2.0 they were not present.

For convention, they are declared using a name in capital letters so to understand at first sight that we are dealing with constants and not variables (properties).

We can define the value of a constant but we cannot modify it.

If we use more words as the constant name, it is of good rule to add an underscore in between the words.

Examples:
- Event.COMPLETE, COMPLETE is a constant of the Event class.
- MouseEvent.CLICK, CLICK is a constant of the MouseEvent class.


Let us look at it with even more examples'

I create a FLA and save it as 'ottava.fla'.
I create the Document Class associated to the FLA and save it as 'Ottava.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Ottava extends MovieClip
	{
		public function Ottava()
		{
			
		}
	}
}
I will now declare the constant:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Ottava extends MovieClip
	{
		private const NOME:String='Filippo';
		
		public function Ottava()
		{
			
		}
	}
}
private const NOME:String='Filippo';

- private :we do know by now the meaning of it
- const :declares to Flash that it is a constant and not a variable
- NOME :the constant's name in capital letter
- :String :is the type of value accepted by the constant (exactly as a variable)
- ='Filippo'; :the constant's value

We can access to the content of the constant in the same way as for the content of a variable:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Ottava extends MovieClip
	{
		private const NOME:String='Filippo';
		
		public function Ottava()
		{
			trace(NOME);
		}
	}
}
with trace(NOME);

I obtain the following output:
Filippo

It is not possible to assign a value to a constant after its declaration.
As an example, I declare another constant:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Ottava extends MovieClip
	{
		private const NOME:String='Filippo';
		private const ETA:int;
		
		public function Ottava()
		{
			trace(NOME);
		}
	}
}
if I publish the SWF, Flash would return the following error:
Warning: 1110: The constant was not initialized.
It simply tells us that we did not assign a value to the constant.

In fact, if we try to assign a value from the building function:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Ottava extends MovieClip
	{
		private const NOME:String='Filippo';
		private const ETA:int;
		
		public function Ottava()
		{
			trace(NOME);
			ETA=35;
		}
	}
}
Flash would return the following error:
1049: Illegal assignment to a variable specified as constant.
It tells us that we are trying to assign a value to a constant in a forbidden way, meaning after the constant's declaration.

About the constant's attributes'
They are the same applied to the variables:
private, internal, protected, public and static
I recommend to look at tutorial 4 if you have any doubt about the variable's attributes.

Source files:
Attached Files
File Type: zip lezione_8.zip (7.1 KB, 30 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 13:50..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 4 - le funzioni Flep Actioscript 3.0 base - tutorials 2 11-11-08 11:18
Tutorial 3 - the loops Flep Actionscript for beginners - tutorials 0 21-11-07 06:50
Tutorial 7 - the packages Flep Object Oriented Programming - tutorials 3 13-11-07 02:42
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 0 01-11-07 06:47
Tutorial Cercasi joyz Actionscript 3.0 base 22 30-07-07 10:52


All times are GMT. The time now is 04:25.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406