views:

62

answers:

1

Right now, this code

var gridArray:Array = [[],[],[],[],[],[]];
gridArray[0][0] = new gridProperties;
//gridProperties has gridX and gridY i.e. gridArray[0][0].gridX

function clearGridSquare(gridSquare:gridProperties):void
{
gridSquare = null;
}

function test():void
{
clearGridSquare(gridArray[0][0]);
trace (gridArray[0][0]);
}

produces

[object gridProperties]

instead of null. I understand that since an object is a reference, even though it technically is passed as a value to the function, it acts like it is passed as a reference. In this way I can alter say, the gridX or gridY properties of the original object from within the function (gridSquare.gridX=1), but I cannot actually make the object itself null. I have found that I can make it null if I do this in the function instead:

gridArray[gridSquare.gridY][gridSquare.gridX] = null;

but that seems like I really bad and roundabout way of doing it. Is there a more elegant solution?

+1  A: 

You can't make the object null because "gridSquare" is a reference to "gridArray[0][0]". So by setting "gridSquare" you are just setting the local reference to the data "gridArray[0][0]" to NULL not the actual data to NULL. I would instead have your function take in the first and second indices into the array you wish to null out. Then in the function you can do...

gridArray[i][j] = null
resolveaswontfix
Alternately, you could store the i and j values in the gridProperties object so that in clearGridSquare, you could call: gridArray[gridSquare.i][gridSquare.j] = null;
thehiatus