Search code examples
c#inheritancepropertiesdeclare

Auto implement properties in inherited form


I'm creating a form to manage the reports of my application, the idea is that every report form inherits from this base form. My problem is that it contains several properties that HAVE to be assigned and i need to verify on every child form if I already called all, so... I'm wondering if there is a way to automatically make a call to all those properties.

This is part of the controller code:

public abstract partial class ReportsController()
{
    public string Table{ get; set; }
    public string Fields{ get; set; }
    public string Condition{ get; set; }
    public string Group{ get; set; }
    public string Order{ get; set; }
    public DataGridViewColumnCollection Columns{ get; set; }
    public SortedList<string, string> ComboboxFields{ get; set; }
    etc...

    protected abstract void New();
    protected abstract void Edit();
    protected abstract void Print();
}

As you can see, methods are not a problem, they are abstract so they will have to be declared (and thanks to Resharper i will be warned if i missed one).

Child form:

public partial class frmReportGuards : ReportsController
{
     public frmReportGuards()
     {
         code...
     }
     protected override void New()
     {
         code...
     }
     other methods...
}

And im looking for this:

public partial class frmReportGuards : ReportsController
{
     public frmReportGuards()
     {
         //Auto assigned properties.
         Table = "";
         Fields = "";
         Condition = "";
         Group = "";
         Order = "";
         Columns = new DataGridViewColumnCollection();
         ComboboxFields = new SortedList<string, string>();
     }
     protected override void New()
     {
         code...
     }
     other methods...
}

I don't know if I'm being senseless here :/ and I really need to get out of this doubt and if is possible... then simplify my work.


Solution

  • Now I understand, if you need to enforce implementation you should do it with abstract properties, inherited classes should implement then, and can be implemented with auto-properties:

    public abstract class A
    {
        public abstract int MyProperty { get; set; }
    }
    
    public class B : A
    {
        public override int MyProperty { get; set; }
    }