FutureCake
FutureCake

Reputation: 2943

call this() constructor and different base() constructor

I have some classes that all extend a parent class. like so:

public abstract class EasyUIELementFoundation
{
    protected GameObject UIElement;
    private RectTransform RectOptions;
    private Vector2 _position;
    private Vector2 _dimensions;

    protected Vector2 Position
    {
        get
        {
            return _position;
        }
        set
        {
            RectOptions.pivot = value;
            _position = value;
        }
    }

    protected Vector2 Dimensions
    {
        get
        {
            return _dimensions;
        }
        set
        {
            RectOptions.sizeDelta = value;
            _dimensions = value;
        }
    }

    protected EasyUIELementFoundation()
    {
        UIElement = new GameObject();
        RectOptions = UIElement.AddComponent(typeof(RectTransform)) as RectTransform;
    }

    protected EasyUIELementFoundation(Vector2 position) : this()
    {
        _position = position;
    }

    protected EasyUIELementFoundation(Vector2 position, Vector2 dimensions) : this()
    {
        _dimensions = dimensions;
        _position = position;
    }
}

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
        UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : this() : base(position)  
    // here is the error it is not possible to call the base.
    {
         //some code...
    }
}

So my problem is i have 3 different base class constuctors and from the derived class i want to call different base constructors, but i cant atm. Is this even possible if not how would i go about doing such a thing?

If anything is unclear let me know so i can clarify.
I am a complete newb to OOP so sorry if this is a bit of a weird question.

Upvotes: 2

Views: 53

Answers (2)

Aleš Doganoc
Aleš Doganoc

Reputation: 12052

You cannot call a this and a base constructor at the same time. Usually the best approach is as already mentioned in the comment to first implement the constructor that is the most complex (has most parameters) and call the base class. Then make the simpler constructor call the complex ones with default values. With this approach your class will then end up looking like this:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : this(default(UnityEngine.Object), default(Vector2))
    {
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)
    {
         UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
         //some code...
    }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

Invoking this() will invoke a base constructor, implicitly or explicitly. Therefore, the other invocation of base would be the second time you construct the same object, which is not allowed.

You can share the code of the constructor by creating a private helper method, like this:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
        InitUiImageComponent();
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)  
    {
        InitUiImageComponent();
         //some code...
    }
    private void InitUiImageComponent() {
        UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
    }

}

In your specific case initialization code could be placed directly into the initializer, so the helper method becomes unnecessary:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent = UIElement.AddComponent(typeof(RawImage));
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)  
    {
         //some code...
    }

}

Upvotes: 1

Related Questions