0
// BaseObjectWindow.cs
public partial class BaseObjectWindow : Window {

    public BaseObjectWindow() {
        // I want to call InitializeComponent() here
    }

    //more constructors

}

// AccountWindow.xaml.cs
public partial class AccountWindow : BaseObjectWindow {

    // I want to remove this whole constructor
    public AccountWindow() : base() {
        InitializeComponent();
    }
}

I want to be able to call InitializeComponent() from the base class so I don't have to call it in the subclasses. The reason I want to do this is because BaseObjectWindow has multiple constructors and C# refuses to implicitly inherit all constructors if you define at least one.

I'm thinking I can save a lot of time by bypassing declaring all constructor for each and every subclass window I need to make (at least 20) but I can't do that if I can't call InitializeComponent() from the super class otherwise the subclass windows initialize as an empty window.

Is there any way to work around this cleanly?

Lorenzo Ang
  • 1,202
  • 3
  • 12
  • 35
  • `SuperClass`, `SubClass` are not proper nomenclature of C#. `initComponents` is not a constructor. What prevents you from calling `InitializeComponent` from the base class? – Sani Huttunen Jan 13 '19 at 15:55
  • Sorry, I made a dirty work around and forgot to clean it up before posting. I decided against it last minute hence asking here. Just edited the code to how it should be. – Lorenzo Ang Jan 13 '19 at 15:57
  • The InitializeComponent method is auto-generated from the XAML of the AccountWindow class. You can't call it in a base class. – Clemens Jan 13 '19 at 16:00
  • I see. I had a feeling as much but thought I'd ask anyway. Thanks guys! – Lorenzo Ang Jan 14 '19 at 04:45

1 Answers1

2

What you can do is declaring a virtual/abstract method in your base class, which you call in your constructors. Override it in your derivered class and call InitializeComponent there.
Note: Calling a virtual method in a constructor can lead to some problems. See also Virtual member call in a constructor

As far as I can tell in this case there shouldn't be any problems, please someone correct me if I am wrong.

public abstract class BaseWindow : Window
{
    protected BaseWindow()
    {
        OnInitialize();
    }

    public abstract void OnInitialize();
}
public partial class DerivedWindow : BaseWindow
{
    public override void OnInitialize()
    {
        InitializeComponent();
    }
}
nosale
  • 808
  • 6
  • 14
  • Doesn't this require each derived class to call `InitializeComponent()`? He wanted to call it from the base class "so I don't have to call it in the subclasses". – redcurry Jan 13 '19 at 17:30
  • @redcurry: Yes it does. Inherintly there is no way of achieving the desired result in WPF since `InitializeComponent` is "autogenerated" for each class. This might be the best option but I'd rather use abstract class and abstract method instead of virtual. – Sani Huttunen Jan 13 '19 at 17:48
  • @SaniSinghHuttunen You are right abstract might be the better approach, changed that – nosale Jan 13 '19 at 17:58
  • Hi Nosale! Thanks again for answering me. The whole point was so I wouldn't have to call it in each `DerivedWindow'. This seems like a clean compromise at least though! – Lorenzo Ang Jan 14 '19 at 04:48