locked
How to inhert from multiple place..I know .net wont support multiple inheritace...but is there any way around RRS feed

  • Question

  • User1094269964 posted

    Lets say I have below class

    public class CallMe

    {

    public int PhoneNumber {get;set;}

    }

    public class MailMe

    {

    public string Email {get;set;}

    }

    Now

    I want to create a class with has all the properites of Callme and MailMe

    like

    public class Communicate :Callme,MailMe

    {

    public string Name {get;set;}

    }

    I dont want CallMe and MailMe as property for Communicate...

    How to do this

    Wednesday, May 24, 2017 5:13 PM

All replies

  • User1068175894 posted

    you are right in that there is no multiple inheritance

    but remember that inheritance is not about reusing properties but modeling the reality, there should be always an "is a" relation between the classes involved.

    With your class names it doesn't make much sense but I assume they are maid up for the example. Here you will ask:  Communicate "is a" CallMe? only if your answer is yes, you will create the relation, not just because you want to reuse the properties.

    Not just because 2 classes have similar properties they necessarily have to have an inheritance relation.

    Other thing, you said you dont want composition(meaning communication having properties callme and phoneme) you should actually favor composition over inheritance, check this article: https://en.wikipedia.org/wiki/Composition_over_inheritance 

    depending on your particular case this may solve your issue:

    public class Communicate 
    {
    public string Name {get;set;}
    }
    
    public class CallMe : Comunicate
    {
    public int PhoneNumber {get;set;}
    }
    
    public class MailMe : Comunicate
    {
    public string Email {get;set;}
    }
    

    Now I will call your classes:

       Communicate: ContactInformation

       CallMe: PhoneInformation

       MailMe: EmailInformation

    --
    Remembert to mark as answer if it solves your issue

    Wednesday, May 24, 2017 5:47 PM