Search code examples
c#multiple-inheritancepartial-classes

Multiple Inheritance for partial class


I'm looking at some code I've inherited. I've added the PrintSpecialInfo() method and get the error show below when trying to call it. I'm confused with the class SpecialPermit. Is it a case of multiple inheritance, both Permit and System.Windows.Forms.UserControl? How to I get access to PrintSpecialInfo from the btnSave_Click method?

Permit.part.cs

public partial class Permit : System.IComparable
{

    public partial class SpecialPermit : Permit
    {
        public virtual void PrintSpecialInfo()
        {
            System.Diagnostics.Trace.WriteLine("Permit.part.cs.PrintSpecialInfoTab --------------------");
            System.Diagnostics.Trace.WriteLine("\tId: " + this.Id);
        }
    }
}

SpecialPermit.cs

public partial class SpecialPermit : System.Windows.Forms.UserControl
{
    private void btnSave_Click (object sender, System.EventArgs e)
    {
        this.PrintSpecialInfo(); // SpecialPermit does not contain a definition for PrintSpecialInfo and no accessible extension method accepting a first argument of type SpecialPermit could be found
    }
}

Solution

  • The way I see this is your "SpecialPermit" is in fact 2 different "SpecialPermit" object types. You can not just add "partial" and expect namespaces to intertwine. Technically the first is NAMESPACE.Permit.SpecialPermit and the other is NAMESPACE.SpecialPermit

    Also, the fact that both the objects have a superclass is a give away that they are not the same. As noted in the commentary, C# does NOT allow multiple inheritance. Thus, unless the "Permit" is a "UserControl" the second definition of SpecialPermit would give a compilation error.

    So to answer, you do not have access to that method because it is not in that object.

    If you want access to it, you need to make sure you are defining/extending the same object. The code you posted defines 2 different "SpecialPermit" object types.