1

I have three different classes in my current project structured as below:

class BaseClass {
    public string prop1;
    public string prop2;
    public string prop3;
}
class C1 : BaseClass {
    public string prop3;   // Common with class C2
    public string prop4;
}
class C2 : BaseClass {
    public string prop3;   // Common with class C1
    public string prop5;
}

I need to have an object which includes prop1, prop2, prop3, prop4, and prop5; but I don't want to have a duplication definition. I don't want to create a new class like this:

class NewClass {
    public string prop1;
    public string prop2;
    public string prop3;
    public string prop4;
    public string prop5;
}

Is there a way (like interface, or abstract class, or anything else) that I can refactor my old classes into so that I can have an object with all 5 properties without defining a new class?

hosjay
  • 901
  • 2
  • 7
  • 19
  • 1
    Possible duplicate of [Multiple Inheritance in C#](http://stackoverflow.com/questions/178333/multiple-inheritance-in-c-sharp) – The_Black_Smurf May 30 '16 at 00:37

2 Answers2

0

Definitely interfaces, just be aware that for matter of theory correctness, you would be implementing methods, not properties, a property is something different in C#. (Link: Properties in C#)

public interface IBaseClass
{
    public string GetProperty1();
    public string GetProperty2();
    public string GetProperty3();
}

public interface IC1
{
    public string GetProperty4();
}

public interface IC2
{
    public string GetProperty5();
}


public class Implementation : IBaseClass, IC1, IC2
{
    public string GetProperty1()
    {
        return "Value";
    }
    public string GetProperty2()
    {
        return "Value";
    }
    public string GetProperty3()
    {
        return "Value";
    }
    public string GetProperty4()
    {
        return "Value";
    }
    public string GetProperty5()
    {
        return "Value";
    }


}

The benefit of doing it like this, is that the implementation class is forced to define such methods.

Oscar Ortiz
  • 813
  • 8
  • 16
0

Will this help?

    class BaseClass
    {
        public virtual string prop1 { get; set; }
        public virtual string prop2 { get; set; }
        public virtual string prop3 { get; set; }
        public virtual string prop4 { get; set; }
        public virtual string prop5 { get; set; }
    }
    class C1 : BaseClass
    {
        public override string prop4 { get; set; }
    }

    class C2 : BaseClass
    {
        public override string prop5 { get; set; }
    }
KDR
  • 478
  • 6
  • 19
  • Thanks but no! In this case C2 has prop4 as a member and I don't want it. I don't want C2 to have any access to prop4. – hosjay May 31 '16 at 03:48
  • Your question says "I need to have an object which includes prop1, prop2, prop3, prop4, and prop5;". can you explain what you meant by that? From your question, I thought objects of any of the classes should have all the properties. – KDR May 31 '16 at 04:29