What is an alternative to inheritance in Partial Class (because already exists) RRS feed

  • Question

  • Say you have 2 two partial classes. One of each of the partial classes derives from a base class. 

    Problem: need to have both classes have same properties that would normally be taken care of with base class.

    partial class One: SomeBaseClass {}
    partial class One {}
    partial class Two: SomeOtherBaseClass {}
    partial class Two{}
    public class HasCommonNeededPropsAndFuncutions
      public string foo {get; set:}
      public string bar {get; set:}

    If the partial classes (without base classes assigned) could inherit from the HasCommon... class that would be great but it's not possible.

    What would be a good solution here? Create a property in each class that initializes a new instance of the HasCommonNeeded... class?

    So... possibly:

    partial class One 
      public HasCommonNeededPropsAndFuncutions Prop1 {get; set;}
    partial class Two
      public HasCommonNeededPropsAndFuncutions  Prop2 {get; set;}

    Thursday, June 28, 2018 9:13 PM

All replies

  • Why can't SomeBaseClass and SomeOtherBaseClass inherit HasCommonNeededPropsAndFunctions?

    If they can't, then yes, including an instance in One and Two would probably be the way to go. The only other possibility that pops to mind would be if HasCommonNeededPropsAndFunctions could be static (like the Math class), then you wouldn't need to inherit or encapsulate.

    • Proposed as answer by BonnieBMVP Friday, June 29, 2018 12:55 AM
    Thursday, June 28, 2018 11:45 PM