Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a structure method inside a non-default structure constructor

Tags:

c#

I have this very simple example that I am using to learn structs in C#:

struct ScreenPosition
{
    // These are the two private members of the structure 
    private int x; 
    private int y;

    private int RangeCheckedX(int xPos)
    {
        if (xPos < 0 || xPos > 1280)
        {
            throw new ArgumentOutOfRangeException("X");
        }
        return xPos;
    }

    private int RangeCheckedY(int yPos)
    {
        if (yPos < 0 || yPos > 1024)
        {
            throw new ArgumentOutOfRangeException("Y");
        }
        return yPos;
    }

    // Declaring the non-default constructor
    public ScreenPosition(int X, int Y)
    {
        this.x = RangeCheckedX(X); // ERROR HERE
        this.y = RangeCheckedY(Y); // ERROR HERE
    }

    // Declaring  the property X - Follows a syntax. See the C# quick reference sheet 
    public int X
    {
        get
        {
            return this.x;
        }

        set
        {
            this.x = RangeCheckedX(value);
        }
    }
    // Declaring  the property X - Follows a syntax. See the C# quick reference sheet 
    public int Y
    {
        get
        {
            return this.y;
        }

        set
        {
            this.y = RangeCheckedY(value);
        }
    }
}

I am getting this error at the "ERROR HERE" comment lines:

The 'this' object cannot be used before all of its fields are assigned to

Is it illegal to call a structure method in the non-default constructor to assign values to the structure members?

like image 509
KalC Avatar asked Jan 01 '26 18:01

KalC


2 Answers

You can make those methods static and it will work, but you cannot call a non-static method until all the fields have been assigned.

like image 156
GBegen Avatar answered Jan 08 '26 23:01

GBegen


It is not allowed to call methods on structs until all fields (properties) are populated.

I know its a hack but this would work.

 struct ScreenPosition
{
    // These are the two private members of the structure 
    private int x;
    private int y;

    private int RangeCheckedX(int xPos)
    {
        if (xPos < 0 || xPos > 1280)
        {
            throw new ArgumentOutOfRangeException("X");
        }
        return xPos;
    }

    private int RangeCheckedY(int yPos)
    {
        if (yPos < 0 || yPos > 1024)
        {
            throw new ArgumentOutOfRangeException("Y");
        }
        return yPos;
    }

    // Declaring the non-default constructor
    public ScreenPosition(int X, int Y)
    {
        this.x = X; 
        this.y = Y; 
        this.x = RangeCheckedX(X);
        this.y = RangeCheckedY(Y);
    }

    // Declaring  the property X - Follows a syntax. See the C# quick reference sheet 
    public int X
    {
        get
        {
            return this.x;
        }

        set
        {
            this.x = RangeCheckedX(value);
        }
    }
    // Declaring  the property X - Follows a syntax. See the C# quick reference sheet 
    public int Y
    {
        get
        {
            return this.y;
        }

        set
        {
            this.y = RangeCheckedY(value);
        }
    }
}
like image 36
Blounty Avatar answered Jan 08 '26 22:01

Blounty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!