I wrote a singleton class to keep track of some variables across my application.
I am getting a syntax error that I can't开发者_如何学Python figure out, I am sure that I am missing something simple but it's been one of those days. Anyone see something wrong with my code?
The error is 1061: Call to a possibly undefined method setResult through a reference with static type Class.
My function in my singleton class
public function setResult(resultNumber:int, value:int): void
{
switch(resultNumber)
{
case 2: { this.result2 = value; break; }
case 3: { this.result3 = value; break; }
case 4: { this.result4 = value; break; }
case 5: { this.result5 = value; break; }
case 6: { this.result6 = value; break; }
case 7: { this.result7 = value; break; }
case 8: { this.result8 = value; break; }
case 9: { this.result9 = value; break; }
case 10: { this.result10 = value; break; }
case 11: { this.result11 = value; break; }
case 12: { this.result12 = value; break; }
case 13: { this.result13 = value; break; }
case 14: { this.result14 = value; break; }
}
}
My function call in my mxml page
if(chkBox1.selected == true)
{
utils.Calculation.setResult(2,1);
}
Thanks in advance for any help!
Assuming you singleton is the Calculation class, are you missing your getInstance call?
utils.Calculation.getInstance().setResult(2, 1);
A good actionscript singleton pattern:
package com.stackOverflow
{
public class MySingleton
{
public function MySingleton(lock:Class)
{
if(lock != SingletonLock)
throw new Error("This class cannot be instantiated, it is a singleton!");
}
private static var mySingleton:MySingleton;
public static function getInstance():MySingleton{
if(mySingleton==null)
mySingleton = new MySingleton(SingletonLock);
return mySingleton;
}
public function setResult(resultNumber:int, value:int): void{
//...
}
}
}
class SingletonLock{}
Edit: getInstance() example for the Calculation class:
private static var calculation:Calculation;
public static function getInstance():Calculation{
if(calculation==null)
calculation = new Calculation(SingletonLock);
return calculation;
}
try this:
public static function setResult(...)
精彩评论