# Not working Constant?

**URL:** <https://forum.kirupa.com/t/not-working-constant/265581>\
**Category:** Uncategorized\
**Created:** [July 10, 2008, 1:15pm UTC](https://forum.kirupa.com/t/not-working-constant/265581 "2008-07-10T13:15:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 1:15pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/1 "2008-07-10T13:15:02Z")

</div>

Hello Everybody,:beer2:

I´m very confused with this peace of code 😑 During the runtime It somehow changed content of constant i have created.

here is fla:

```auto
var _currentBlock:Tetris_Shape;
trace(Tetris_Shape._POS_IN_GRID); **[COLOR=Red]//output 3,1,3,2,3,3,3,4,3,1,3,2,3,3,2,3,2,1,2,2,2,3,3,3,3,3,3,2,2,2,2,3,2,3,3,3,3,2,2,4,[COLOR=RoyalBlue]2,3,3,3,4,3,3,2,[/COLOR]3,3,2,3,2,2,1,2[/COLOR]**
var someArray:Array = Tetris_Shape.rewriteBlockData(5, Tetris_Shape._POS_IN_GRID);

trace(Tetris_Shape._POS_IN_GRID);**[COLOR=Red] //output 3,1,3,2,3,3,3,4,3,1,3,2,3,3,2,3,2,1,2,2,2,3,3,3,3,3,3,2,2,2,2,3,2,3,3,3,3,2,2,4,[COLOR=RoyalBlue]2,2,2,3,2,4,3,3,[/COLOR]3,3,2,3,2,2,1,2[/COLOR]**

```

and here is as:

```auto
package {
    
    public class Tetris_Shape {

        public static const _POS_IN_GRID:Array = new Array(
                                                    [[3,1],[3,2],[3,3],[3,4]],
                                                    [[3,1],[3,2],[3,3],[2,3]],
                                                    [[2,1],[2,2],[2,3],[3,3]],
                                                    [[3,3],[3,2],[2,2],[2,3]],
                                                    [[2,3],[3,3],[3,2],[2,4]],
                                                    [[2,3],[3,3],[4,3],[3,2]],
                                                    [[3,3],[2,3],[2,2],[1,2]]
                                                    );

        public static function rewriteBlockData (number:uint, carrier:Array):Array {
            var mirroredNumbers:Array = new Array(5,4,3,2,1,0);
            var easyNum:uint;
            for(var i:uint = 0; i<4; i++){
                easyNum = carrier[number]*[0];
                carrier[number]*[0] = mirroredNumbers[carrier[number]*[1]];
                carrier[number]*[1] = easyNum;
            }
            return carrier;
        }
    }
}

```

Thank you for eventually responses \>:)

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 10, 2008, 1:33pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/2 "2008-07-10T13:33:32Z")

</div>

That’s pretty bizarre that it changes the constant instead of throwing an error.

var someArray:Array = Tetris\_Shape.rewriteBlockData(5, Tetris\_Shape.\_POS\_IN\_GRID.concat());

would solve the problem tho…

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 10, 2008, 2:39pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/3 "2008-07-10T14:39:34Z")

</div>

const only applies to the value of \_POS\_IN\_GRID, not any of its properties

---

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 2:51pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/4 "2008-07-10T14:51:50Z")

</div>

Thank you, but it didn´t helped. 🙂

---

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 2:55pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/5 "2008-07-10T14:55:40Z")

</div>

Thank you very much, but don´t you know, how is it even possible that this property was changed … I haven´t done anything to change this property. 🙂

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 10, 2008, 3:04pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/6 "2008-07-10T15:04:49Z")

</div>

When you use const, you’re saying that no one can change the value of that property for that object. In your case:

```auto
var _currentBlock:Tetris_Shape;
Tetris_Shape._POS_IN_GRID = new Array(); // ERROR, const enforced!

```

It has no way of affecting or recognizing any of the values within _that_ value. If you wanted every one of those array values to be const, you would have to change that object from an array to a custom class and define each one of those values in a new const property.

Make sense?

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 10, 2008, 3:08pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/7 "2008-07-10T15:08:11Z")

</div>

change to this then;

```auto

        public static function rewriteBlockData (number:uint, xcarrier:Array):Array {
            var carrier:Array = xcarrier.concat();
            var mirroredNumbers:Array = new Array(5,4,3,2,1,0);
            var easyNum:uint;
            for(var i:uint = 0; i<4; i++){
                easyNum = carrier[number]*[0];
                carrier[number]*[0] = mirroredNumbers[carrier[number]*[1]];
                carrier[number]*[1] = easyNum;
            }
            return carrier;
        }

```

---

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 3:10pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/8 "2008-07-10T15:10:23Z")

</div>

Yes it does,

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 3:16pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/9 "2008-07-10T15:16:32Z")

</div>

**[COLOR=“Black”]sekasi:[/COLOR]**

Strange, but it dosen´t work either.

---

<div class="post-metadata">

**Author:** ![Dart](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dart](https://forum.kirupa.com/u/Dart)\
**Post date:** [July 10, 2008, 3:21pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/10 "2008-07-10T15:21:26Z")

</div>

I have another question 🙂

Do you know, why \_POS\_IN\_GRID was changed, or what changed it ?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 10, 2008, 3:24pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/11 "2008-07-10T15:24:33Z")

</div>

As for the problem itself, const isn’t what you really need. What you need (int he context of your current code) is a deep copy of your array. The easiest solution for that might be changing \_POS\_IN\_GRID to a method that returns that array where each time it’s called, a new instance of that array is returned…

```auto

        public static function _POS_IN_GRID():Array { return new Array(
                                                    [[3,1],[3,2],[3,3],[3,4]],
                                                    [[3,1],[3,2],[3,3],[2,3]],
                                                    [[2,1],[2,2],[2,3],[3,3]],
                                                    [[3,3],[3,2],[2,2],[2,3]],
                                                    [[2,3],[3,3],[3,2],[2,4]],
                                                    [[2,3],[3,3],[4,3],[3,2]],
                                                    [[3,3],[2,3],[2,2],[1,2]]
                                                    );}

```

I haven’t examined the code too thoroughly but that should probably do it.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 10, 2008, 3:29pm UTC](https://forum.kirupa.com/t/not-working-constant/265581/12 "2008-07-10T15:29:38Z")

</div>

[QUOTE=Dart;2356233]I have another question 🙂

Do you know, why \_POS\_IN\_GRID was changed, or what changed it ?[/QUOTE]

rewriteBlockData changed it. You passed it in to rewriteBlockData as carrier and that function is adding values to it.

sekasi started off with the right solution, trying to make a copy of the array through xcarrier.concat(); The problem with that is that it is not a deep copy - or a copy that copies the array and creates copies of all the objects in the array. Instead, you get a copy of the main array, but the reference values it contains references the same nested arrays as the original. Now, you could loop through and copy each one of those over again with another concat(), but that could be a bit of a pain. Just recreating the array from scratch with the method I suggested earlier might be all you need since it creates a completely new array for each use.
