0

I have a setter for a vector that works properly, but I'm trying to make it do some additional things when one of the elements is changed.

private var _myVect:Vector.<int> = new <int>[0,0,0,0];

public function set myVect(newVect:Vector.<int>):void {
    trace(newVect);
    _myVect = newVect;
}

If I do myVect[0] = 1, then _myVect becomes [1,0,0,0] but it doesn't even trace. How can I get the element number and assigned value from inside the setter?

BladePoint
  • 632
  • 5
  • 16

1 Answers1

0

It doesn't trace because when using "myVector[0] = ", you're actually using the getter, not the setter (You GET the vector to then SET one of it s values).

I would only implement a getter.

Instead of myVect[0] = 1 you should do _myVect[0] = 1 or implement a public method like so:

public function updateVectorAt(value:int, index:int):void
{ 
    _myVect[index] = value; 
    // do the other stuff here...
}
mika
  • 1,411
  • 1
  • 12
  • 23
  • 1
    This does not explain the issue at all. All you are doing is telling him how to do it without setters and getters. – The_asMan Oct 02 '13 at 15:00
  • 1
    @The_asMan have you properly read my answer? He believes he uses the setter when in fact he s using the getter to the set a value within the vector. The setter for a vector would be needed only to overwrite the full object, not a single value in it. – mika Oct 02 '13 at 15:01
  • @The_asMan I don't understand what bothers you... what is your correct answer then AS man?? – mika Oct 02 '13 at 15:04
  • First off there is no getter. Since it is not defined do know what happens then? – The_asMan Oct 02 '13 at 15:06
  • LOL if he had no getters "myVect[0] = 1" wouldn't work sir... I m done arguing with you. prove a point or stop bothering... – mika Oct 02 '13 at 15:07
  • And my point in the first comment was for you to understand there is a reason to use setters and getters or one without the other. The OP clearly wants to use a setter so fix the issue by using the setter not by doing a complete rewrite – The_asMan Oct 02 '13 at 15:09
  • How about this from the funny guy. myVect = (myVect[0] = 1); Wow its fixed and all it took was changing one line of code – The_asMan Oct 02 '13 at 15:19
  • I was taking your assumption that there is either way your answer is still wrong so here is a -1 – The_asMan Oct 02 '13 at 15:22
  • 2
    :-/ Dude if you have a better answer you post it... that s how Stackoverflow works... ridiculous! – mika Oct 02 '13 at 15:23
  • There is a getter, but I didn't think it was relevant to post. I can't do _myVect[0] = 1 because it's private. It seems Mika's updateVectorAt method is the way to go, but I really wish I could do it in a setter. Is there a way to make a setter just for changing individual elements of the vector? – BladePoint Oct 03 '13 at 03:19
  • Individually named setters for each element in the Vector. eg: public function set myVect0(p_value:int):void { _myVect[0] = p_value }; – moosefetcher Oct 03 '13 at 14:31