0
votes

I need to execute derived class constructor before base class constructor. I am attaching code which is using virtual object in base class which need to be initialised in derived class. We decide type of virtual object in derived class and then assign values to that object once we have type of that object. How could I call derived class constructor before base class constructor in this scenario.

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        this.BaseObject.Title = config.Title;
        this.BaseObject.GroupID = config.GroupID;
    }
}

public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public override ObjectBase BaseObject
    {
        get { return audio_objectAction; }
        set
        {
            audio_objectAction = (Audio_MonitorAction)value;
            NotifyPropertyChanged();
        }
    }

    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
        audio_objectAction = new Audio_MonitorAction(createGuid);
    }
}
2
I am having the same problem. How are you supposed to use virtual methods in a base constructor, why was this crap error implemented anyways. You don't need to call the base constructor first in C++, why do you need to do it a very loosely coupled language like javascript. - Nick Sotiros

2 Answers

1
votes

"execute derived class constructor before base class constructor" is impossible

if possible, move initialization into BaseObject property (why should it be virtual?)

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    private ObjectBase baseObject;
    public ObjectBase BaseObject
    {
        get { return baseObject; }
        set
        {
            baseObject = value;
            if (baseObject != null)
            {
                baseObject.Title = config.Title;
                baseObject.GroupID = config.GroupID;
            }
            NotifyPropertyChanged();
        }
    }

    XmlElementConfig config;
    public void BaseClass(XmlElementConfig config)
    {
        this.config = config;
    }
}
public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
        BaseObject = audio_objectAction = new Audio_MonitorAction(createGuid);
    }
}
1
votes
public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        InitBase(config);
    }
    protected void InitBase(XmlElementConfig config)
    {
        if (BaseObject != null)
        {
            BaseObject.Title = config.Title;
            BaseObject.GroupID = config.GroupID;
        }
    }
}
public DerivedClass(XmlElementConfig config) : base(config)
{
    InitializeComponent();
    audio_objectAction = new Audio_MonitorAction(createGuid);
    InitBase(config);
}

Alternatively you can do in BaseClass call to virtual function and override it in derived class.

public class BaseClass : UserControl, INotifyPropertyChanged
{
    public Path ConnIn;
    public Path ConnOut;

    public virtual ObjectBase BaseObject { get; set; }

    public void BaseClass(XmlElementConfig config)
    {
        InitDerivedClass();
        if (BaseObject != null)
        {
            BaseObject.Title = config.Title;
            BaseObject.GroupID = config.GroupID;
        }
    }
    protected virtual void InitDerivedClass() {}
}
public partial class DerivedClass : CanvasBase
{
    private Audio_MonitorAction audio_objectAction;

    public override ObjectBase BaseObject
    {
        get { return audio_objectAction; }
        set
        {
            audio_objectAction = (Audio_MonitorAction)value;
            NotifyPropertyChanged();
        }
    }
    protected override ObjectBase InitDerivedClass
    {
        audio_objectAction = new Audio_MonitorAction(createGuid);
    }
    public DerivedClass(XmlElementConfig config) : base(config)
    {
        InitializeComponent();
    }
}