> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:flashcoders-
> [EMAIL PROTECTED] On Behalf Of Paul Steven
> Sent: Saturday, November 04, 2006 2:39 AM
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Copy Constructor
> 
> Couldn't quite figure out your code Mike so have just rewritten the
copy
> function based on what you said:

Well, that was the most important part.

> I am however curious to understand the BoardStatus class you wrote.
You
> declared a variable of type BoardStatus within BoardStatus. Can you
> explain
> what you are doing here please.


This was an example of an enumeration class. As I recall, your code had
been storing the status of each Connect-4 square (red, black, or empty)
as a number (0, 1, or 2). The problem with this is that it's easy to
slip it a bad number (e.g., 3, 4, 5, etc.). One solution is to create an
enumeration class.

An enumeration class has a private constructor and a finite number of
instances, generally static constants (or pseudo-constants) associated
with the class. Thus, it is impossible to assign an invalid value to
anything that requires a variable of that class type.

Example (somewhat modified from my previous example):

class connect4.SquareStatus {
        private function SquareStatus() {
        }
        public static var BLACK:SquareStatus = new SquareStatus();
        public static var EMPTY:SquareStatus = new SquareStatus();
        public static var RED:SquareStatus = new SquareStatus();
}
// Elsewhere:
var status:SquareStatus; // It is impossible to set this variable to
anything but null, undefined, or a valid SquareStatus value.
status = SquareStatus.RED;

Incidentally, you could skip the EMPTY value and just have null signify
an empty square.

In AS3.0, this becomes trickier, since there are no private
constructors. Anyone know if there is a recommended way to get around
this? (On the plus side, you can make the class final and the static
values constant, which is a major shortcoming in AS2.0.)
--
Mike Keesey

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to