[Flashcoders] Private Static -static

2006-03-27 Thread CK

Hi,

In the following code: I'm getting the following static from the  
ouput window:
**Error** /Users/user/Desktop/code/oop_review/BOX/Box.as: Line 34:  
Instance variables cannot be accessed in static functions.

return width*height;

Total ActionScript Errors: 1 Reported Errors: 1

How could I access these instance variables from within this private  
static method? Is it advisable using Private Static? Would I have to  
create an instance of the class then access the properties?




//AS
class Box {
//accessing a static property through a method.
//The box class, with methods to set and retreive the
//value of the class property, numSides.
//
//The class property numSides
private static var numSides:Number = 4;
//
private var width:Number;
private var height:Number;
private var area:Number;
//
//all CAPS denotes constants, values that never change.
private static var DEFAULT_WIDTH:Number = 30;
private static var DEFAULT_HEIGHT:Number = 20;
//
public static var maxWidth = 250;
public static var maxHeight = 250;
//
//The constructor function.
public function Box(w:Number, h:Number) {
if (w == undefined) {
w = DEFAULT_WIDTH;
}
if (h == undefined) {
h = DEFAULT_HEIGHT;
}
width = w;
height = h;
//initialize area. This is perfectly legal within a constructor
//area = width*height;
}
private static function getArea():Number {
return width*height;
}
//Method for setting numSides.
public function setNumSides(newNumSides:Number):Void {
numSides = newNumSides;
}
//Method for getting numSides.
public function getNumSides():Number {
return numSides;
}
//Accessor to retreive width
public function getWidth():Number {
return width;
}
//Accessor to assign width
public function setWidth(w:Number):Void {
width = w;
}
//Accessor to retreive height
public function getHeight():Number {
return height;
}
//Accessor to set height
public function setHeight(h:Number):Void {
height = h;
}
//reset the dimensions
public function resetDimensions():Void {
height = 1;
width = 1;
trace(Dimensions reset !);
}
public function onMouseDown():Void {
resetDimensions();
}
public function enableReset():Void {
Mouse.addListener(this);
}
public function disableReset():Void {
Mouse.removeListener(this);
}
//this gurantees a safe means of deleting its instances.
public function die():Void {
disableReset();
//Unregister the object so the Mouse class
//deletes the reference to it.  
}
}
//End AS
___
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


Re: [Flashcoders] Private Static -static

2006-03-27 Thread Weldon MacDonald
Your trying to access the properties width and heigh from a static
function getArea. The problem is a static function is designed to
handle class wide properties and width and height are local to each
insttance of the class. The compiler won't allow it. Remove the static
from the function definition, I don't see why they would be needed
there anyway.

On 3/27/06, CK [EMAIL PROTECTED] wrote:
 Hi,

 In the following code: I'm getting the following static from the
 ouput window:
 **Error** /Users/user/Desktop/code/oop_review/BOX/Box.as: Line 34:
 Instance variables cannot be accessed in static functions.
 return width*height;

 Total ActionScript Errors: 1 Reported Errors: 1

 How could I access these instance variables from within this private
 static method? Is it advisable using Private Static? Would I have to
 create an instance of the class then access the properties?



 //AS
 class Box {
 //accessing a static property through a method.
 //The box class, with methods to set and retreive the
 //value of the class property, numSides.
 //
 //The class property numSides
 private static var numSides:Number = 4;
 //
 private var width:Number;
 private var height:Number;
 private var area:Number;
 //
 //all CAPS denotes constants, values that never change.
 private static var DEFAULT_WIDTH:Number = 30;
 private static var DEFAULT_HEIGHT:Number = 20;
 //
 public static var maxWidth = 250;
 public static var maxHeight = 250;
 //
 //The constructor function.
 public function Box(w:Number, h:Number) {
 if (w == undefined) {
 w = DEFAULT_WIDTH;
 }
 if (h == undefined) {
 h = DEFAULT_HEIGHT;
 }
 width = w;
 height = h;
 //initialize area. This is perfectly legal within a 
 constructor
 //area = width*height;
 }
 private static function getArea():Number {
 return width*height;
 }
 //Method for setting numSides.
 public function setNumSides(newNumSides:Number):Void {
 numSides = newNumSides;
 }
 //Method for getting numSides.
 public function getNumSides():Number {
 return numSides;
 }
 //Accessor to retreive width
 public function getWidth():Number {
 return width;
 }
 //Accessor to assign width
 public function setWidth(w:Number):Void {
 width = w;
 }
 //Accessor to retreive height
 public function getHeight():Number {
 return height;
 }
 //Accessor to set height
 public function setHeight(h:Number):Void {
 height = h;
 }
 //reset the dimensions
 public function resetDimensions():Void {
 height = 1;
 width = 1;
 trace(Dimensions reset !);
 }
 public function onMouseDown():Void {
 resetDimensions();
 }
 public function enableReset():Void {
 Mouse.addListener(this);
 }
 public function disableReset():Void {
 Mouse.removeListener(this);
 }
 //this gurantees a safe means of deleting its instances.
 public function die():Void {
 disableReset();
 //Unregister the object so the Mouse class
 //deletes the reference to it.
 }
 }
 //End AS
 ___
 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



--
Weldon Mac Donald
___
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