Re: [Flashcoders] Question on preventing this array[n] = 'whatever' on Array var
Thanx. Well i could extend Array and then override all the methods and have them dispatch events, like ON_CHANGE. Overwriting all the public mehtods is a pain, so I suspect i can use the flash.utils.Proxy for this. I just need to understand this utils.Proxy and flash_proxy thing. Could somebody hint me if this would be worth looking into for this particular case? Jiri Glen Pike wrote: Sounds like you want to use a kind of "Observer" pattern: Can you "wrap" up your array class in something that implements the methods you want, ignores / passes on the stuff you care about and then notifiies listeners of any changes that you care about? Flex makes this quite easy with {watching} objects virtually automatically - not sure if there is something in PureAS3 that would help without requiring some huge framework. The safest way of doing this though might be to prevent any external class accessing your array. Make your class implement some kind of "ArrayInterface" and allow the calling classes to use your class rather than your array to add and remove things? Glen Jiri wrote: Hello list, Lets say I have a class with a public getter to a private var which holds an Array. Some other class can do this then: var tArr:Array = class.myArray; I can then do this: tArr.push(int) and the content of the array has changed without the class knowing. This is in most cases I use it ok, but now I am wondering how to deal with it when I really want to create a read only array. I am aware that I can copy it, but then this can be very expensive when it is requested a lot of times and it is a relative big array. So i was thinking the following. class A{ private var pArray:Array = [1,2,3] public function get list():listArray{ return new listArray(pArray); } } class listArray{ private var pList:Array public function listArray(tArr:Array):void{ pList = listArray } override function push():void{ throw new IllegalOperationError() } } I would have to override all the public methods of Array which can be a pain. My question is. How would I prevent users from doing this: listArray[n] = 'whatever' Much appreciated Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on preventing this array[n] = 'whatever' on Array var
Sounds like you want to use a kind of "Observer" pattern: Can you "wrap" up your array class in something that implements the methods you want, ignores / passes on the stuff you care about and then notifiies listeners of any changes that you care about? Flex makes this quite easy with {watching} objects virtually automatically - not sure if there is something in PureAS3 that would help without requiring some huge framework. The safest way of doing this though might be to prevent any external class accessing your array. Make your class implement some kind of "ArrayInterface" and allow the calling classes to use your class rather than your array to add and remove things? Glen Jiri wrote: Hello list, Lets say I have a class with a public getter to a private var which holds an Array. Some other class can do this then: var tArr:Array = class.myArray; I can then do this: tArr.push(int) and the content of the array has changed without the class knowing. This is in most cases I use it ok, but now I am wondering how to deal with it when I really want to create a read only array. I am aware that I can copy it, but then this can be very expensive when it is requested a lot of times and it is a relative big array. So i was thinking the following. class A{ private var pArray:Array = [1,2,3] public function get list():listArray{ return new listArray(pArray); } } class listArray{ private var pList:Array public function listArray(tArr:Array):void{ pList = listArray } override function push():void{ throw new IllegalOperationError() } } I would have to override all the public methods of Array which can be a pain. My question is. How would I prevent users from doing this: listArray[n] = 'whatever' Much appreciated Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Question on preventing this array[n] = 'whatever' on Array var
Hello list, Lets say I have a class with a public getter to a private var which holds an Array. Some other class can do this then: var tArr:Array = class.myArray; I can then do this: tArr.push(int) and the content of the array has changed without the class knowing. This is in most cases I use it ok, but now I am wondering how to deal with it when I really want to create a read only array. I am aware that I can copy it, but then this can be very expensive when it is requested a lot of times and it is a relative big array. So i was thinking the following. class A{ private var pArray:Array = [1,2,3] public function get list():listArray{ return new listArray(pArray); } } class listArray{ private var pList:Array public function listArray(tArr:Array):void{ pList = listArray } override function push():void{ throw new IllegalOperationError() } } I would have to override all the public methods of Array which can be a pain. My question is. How would I prevent users from doing this: listArray[n] = 'whatever' Much appreciated Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders