Class constructor

Hi, there. i’m sorry to my poor knowlage.
in my opinion, if call a constructor, the parameter should be same as the constructor definition. but i learned from this lecture, it seems it is not like as my mind. GridObject is a contructor class

public class GridSystem
{
    private int width;//declare the grid width
    private int height;//declare the gird height
    private float cellSize;
    private GridObject[,] gridObjectsArray;//declare an array include gridsystem and gridposition
    public GridSystem(int _width, int _height, float _cellSize)//class constructor
    {
        this.width = _width;
        this.height = _height;
        this.cellSize = _cellSize;
        *gridObjectsArray = new GridObject[this.width, this.height];//assign gridobjectarray[width,height]*
*        **//but without constuctor parameter???***

        for (int x = 0; x < _width; x++)//iterate the width
        {
            for (int z = 0; z < _height; z++)//iterate the height
            {
                //drawling(startpostion each grid, endpostion each grid,color,show time);
                //Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x, z) + Vector3.right * .2f, Color.white, 1000f);

                GridPosition gridPosition = new GridPosition(x, z);
                gridObjectsArray[x, z] = new GridObject(this, gridPosition);
            }
        }


    }
    ```

The gridObjectsArray isn’t a GridObject, it’s actually an Array of GridObjects. The declaration

gridObjectsArray = new GridObject[this.width, this.height];

Simply creates the array, but does not put any actual GridObjects in the array. Think of it like putting up a mailbox with empty boxes. It’s just a container. What’s in side the mailbox is either nothing (null) or a piece of mail. It’s the same with our gridObjectsArray. There’s no Grid Object in it until one is put there (within the for loop).

thank you very much, i got it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms