Re: [Flashcoders] Why does this work?!
Oh, it works correctly, assuming I'm not dumb and give a couple "constants" the same values. It just seems strange for this to be allowed. Thus the question "why does this work". I would think it shouldn't even compile. -Andy On 4/17/07, Hans Wichman <[EMAIL PROTECTED]> wrote: Hi, what were your own test results? :) Yes you can do this, and flash will execute the first matching case statement. Any other matches will be ignored. greetz JC On 4/17/07, Andy Herrman <[EMAIL PROTECTED]> wrote: > > I just realized that there are a number of switch statements in my > code which probably shouldn't work, yet appear to, and I'm wondering > why. > > Here's a really simple example. I have a class that tracks the > connection state of my app, with the following values used as the > states (read-only attributes simulating constants): > > public static function get CONNECTED():String { return "CONNECTED"; } > public static function get FAILURE():String { return "FAILURE"; } > public static function get NOT_CONNECTED():String { return > "NOT_CONNECTED"; } > > In the code that lets you set the state to a particular value it does > a sanity check to make sure the state value is one that's expected > (since in theory the user could provide any string value): > > public function setConnectionState(cs:String):Void { >switch(cs) { > case ConnectionState.CONNECTED: > case ConnectionState.FAILURE: > case ConnectionState.NOT_CONNECTED: >break; > default: >cs = ConnectionState.NOT_CONNECTED; >break; >} >this._connectionState = cs; > } > > Now in Java switch statements must use constants for the case values. > You can do something like I just did, but the variables being > referenced must be declared final (so the compiler knows they won't > change). There isn't any equivalent to this in Flash (I simulate > constants by doing read only properties), so why does the case > statement work? Does flash actually execute the stuff after the > 'case' keyword? What happens if multiple of those things return the > same value (for instance, say both CONNECTED and FAILURE returned > "foo")? > > -Andy > ___ > 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 > ___ 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 ___ 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] Why does this work?!
Hello :) in AS2 you can create constants with the ASSetProgFlags global method : public static var CONNECTED:String = "CONNECTED" ; public static var FAILURE:String= "FAILURE" ; public static var NOT_CONNECTED:String = "NOT_CONNECTED"; private static var __ASPF__ = _global.ASSetPropFlags( ConnectionState , null , 7, 7 ) ; ConnectionState is the name of your enumeration static class :) For me.. your code is ok : public function setConnectionState(cs:String):Void { switch(cs) { case ConnectionState.CONNECTED : case ConnectionState.FAILURE : { break; } default : { cs = ConnectionState.NOT_CONNECTED ; } } this._connectionState = cs ; } EKA+ :) 2007/4/17, Andy Herrman <[EMAIL PROTECTED]>: I just realized that there are a number of switch statements in my code which probably shouldn't work, yet appear to, and I'm wondering why. Here's a really simple example. I have a class that tracks the connection state of my app, with the following values used as the states (read-only attributes simulating constants): public static function get CONNECTED():String { return "CONNECTED"; } public static function get FAILURE():String { return "FAILURE"; } public static function get NOT_CONNECTED():String { return "NOT_CONNECTED"; } In the code that lets you set the state to a particular value it does a sanity check to make sure the state value is one that's expected (since in theory the user could provide any string value): public function setConnectionState(cs:String):Void { switch(cs) { case ConnectionState.CONNECTED: case ConnectionState.FAILURE: case ConnectionState.NOT_CONNECTED: break; default: cs = ConnectionState.NOT_CONNECTED; break; } this._connectionState = cs; } Now in Java switch statements must use constants for the case values. You can do something like I just did, but the variables being referenced must be declared final (so the compiler knows they won't change). There isn't any equivalent to this in Flash (I simulate constants by doing read only properties), so why does the case statement work? Does flash actually execute the stuff after the 'case' keyword? What happens if multiple of those things return the same value (for instance, say both CONNECTED and FAILURE returned "foo")? -Andy ___ 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 ___ 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] Why does this work?!
Hi, what were your own test results? :) Yes you can do this, and flash will execute the first matching case statement. Any other matches will be ignored. greetz JC On 4/17/07, Andy Herrman <[EMAIL PROTECTED]> wrote: I just realized that there are a number of switch statements in my code which probably shouldn't work, yet appear to, and I'm wondering why. Here's a really simple example. I have a class that tracks the connection state of my app, with the following values used as the states (read-only attributes simulating constants): public static function get CONNECTED():String { return "CONNECTED"; } public static function get FAILURE():String { return "FAILURE"; } public static function get NOT_CONNECTED():String { return "NOT_CONNECTED"; } In the code that lets you set the state to a particular value it does a sanity check to make sure the state value is one that's expected (since in theory the user could provide any string value): public function setConnectionState(cs:String):Void { switch(cs) { case ConnectionState.CONNECTED: case ConnectionState.FAILURE: case ConnectionState.NOT_CONNECTED: break; default: cs = ConnectionState.NOT_CONNECTED; break; } this._connectionState = cs; } Now in Java switch statements must use constants for the case values. You can do something like I just did, but the variables being referenced must be declared final (so the compiler knows they won't change). There isn't any equivalent to this in Flash (I simulate constants by doing read only properties), so why does the case statement work? Does flash actually execute the stuff after the 'case' keyword? What happens if multiple of those things return the same value (for instance, say both CONNECTED and FAILURE returned "foo")? -Andy ___ 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 ___ 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
[Flashcoders] Why does this work?!
I just realized that there are a number of switch statements in my code which probably shouldn't work, yet appear to, and I'm wondering why. Here's a really simple example. I have a class that tracks the connection state of my app, with the following values used as the states (read-only attributes simulating constants): public static function get CONNECTED():String { return "CONNECTED"; } public static function get FAILURE():String { return "FAILURE"; } public static function get NOT_CONNECTED():String { return "NOT_CONNECTED"; } In the code that lets you set the state to a particular value it does a sanity check to make sure the state value is one that's expected (since in theory the user could provide any string value): public function setConnectionState(cs:String):Void { switch(cs) { case ConnectionState.CONNECTED: case ConnectionState.FAILURE: case ConnectionState.NOT_CONNECTED: break; default: cs = ConnectionState.NOT_CONNECTED; break; } this._connectionState = cs; } Now in Java switch statements must use constants for the case values. You can do something like I just did, but the variables being referenced must be declared final (so the compiler knows they won't change). There isn't any equivalent to this in Flash (I simulate constants by doing read only properties), so why does the case statement work? Does flash actually execute the stuff after the 'case' keyword? What happens if multiple of those things return the same value (for instance, say both CONNECTED and FAILURE returned "foo")? -Andy ___ 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] Why does this work?!
Oh, the code is OK, and it works. I'm just surprised Flash lets you have case statements with non-constant values. Since the case values are actually functions that are evaluated it's possible that you can have multiple case statements of the same value. I'm not used to languages/compilers allowing those situations. -Andy On 4/17/07, eka <[EMAIL PROTECTED]> wrote: Hello :) in AS2 you can create constants with the ASSetProgFlags global method : public static var CONNECTED:String = "CONNECTED" ; public static var FAILURE:String= "FAILURE" ; public static var NOT_CONNECTED:String = "NOT_CONNECTED"; private static var __ASPF__ = _global.ASSetPropFlags( ConnectionState , null , 7, 7 ) ; ConnectionState is the name of your enumeration static class :) For me.. your code is ok : public function setConnectionState(cs:String):Void { switch(cs) { case ConnectionState.CONNECTED : case ConnectionState.FAILURE : { break; } default : { cs = ConnectionState.NOT_CONNECTED ; } } this._connectionState = cs ; } EKA+ :) 2007/4/17, Andy Herrman <[EMAIL PROTECTED]>: > > I just realized that there are a number of switch statements in my > code which probably shouldn't work, yet appear to, and I'm wondering > why. > > Here's a really simple example. I have a class that tracks the > connection state of my app, with the following values used as the > states (read-only attributes simulating constants): > > public static function get CONNECTED():String { return "CONNECTED"; } > public static function get FAILURE():String { return "FAILURE"; } > public static function get NOT_CONNECTED():String { return > "NOT_CONNECTED"; } > > In the code that lets you set the state to a particular value it does > a sanity check to make sure the state value is one that's expected > (since in theory the user could provide any string value): > > public function setConnectionState(cs:String):Void { > switch(cs) { > case ConnectionState.CONNECTED: > case ConnectionState.FAILURE: > case ConnectionState.NOT_CONNECTED: > break; > default: > cs = ConnectionState.NOT_CONNECTED; > break; > } > this._connectionState = cs; > } > > Now in Java switch statements must use constants for the case values. > You can do something like I just did, but the variables being > referenced must be declared final (so the compiler knows they won't > change). There isn't any equivalent to this in Flash (I simulate > constants by doing read only properties), so why does the case > statement work? Does flash actually execute the stuff after the > 'case' keyword? What happens if multiple of those things return the > same value (for instance, say both CONNECTED and FAILURE returned > "foo")? > >-Andy > ___ > 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 > ___ 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 ___ 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] Why does this work in Flash 6 Actionscript 2 butnot Flash 7 Actionscript 2 ...
> > Why does the following work when published as Flash 6 > Actionscript 2, but not Flash 7 actionscript 2 (or Flash 8 > Actionscript 2).var nInt:Number;var oMain:Object = {};var > nCounter:Number;var sClipName:String;var > nClipDepth:Number;function spawnClip():Void{var > mcTemp:MovieClip = this.attachMovie("mcCircle", > "mcCircle"+nCounter, nCounter, oMain);mcTemp._x = > Math.random()*600;mcTemp._y = Math.random()*400; > nCounter++;}nInt = setInterval(this, "spawnClip", > 500); Posting code without carriage returns makes baby Jesus cry. ___ 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] Why does this work in Flash 6 Actionscript 2 but not Flash 7 Actionscript 2 ...
Hi Stephen, You are incrementing nCounter without ever initializing it. In Flash 6 and earlier if you incremented an undefined value it treated it as 0, but in Flash 7 and later it remains undefined. Set nCounter to 0 first and all should be well. Jake On 11/12/06, Stephen Ford <[EMAIL PROTECTED]> wrote: Why does the following work when published as Flash 6 Actionscript 2, but not Flash 7 actionscript 2 (or Flash 8 Actionscript 2).var nInt:Number;var oMain:Object = {};var nCounter:Number;var sClipName:String;var nClipDepth:Number;function spawnClip():Void{var mcTemp:MovieClip = this.attachMovie("mcCircle", "mcCircle"+nCounter, nCounter, oMain);mcTemp._x = Math.random()*600;mcTemp._y = Math.random()*400;nCounter++;}nInt = setInterval(this, "spawnClip", 500);Thanks,Stephen.___ 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 ___ 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
[Flashcoders] Why does this work in Flash 6 Actionscript 2 but not Flash 7 Actionscript 2 ...
Why does the following work when published as Flash 6 Actionscript 2, but not Flash 7 actionscript 2 (or Flash 8 Actionscript 2).var nInt:Number;var oMain:Object = {};var nCounter:Number;var sClipName:String;var nClipDepth:Number;function spawnClip():Void{var mcTemp:MovieClip = this.attachMovie("mcCircle", "mcCircle"+nCounter, nCounter, oMain); mcTemp._x = Math.random()*600;mcTemp._y = Math.random()*400; nCounter++;}nInt = setInterval(this, "spawnClip", 500);Thanks,Stephen.___ 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