1

Say I have a Father class to be inherited by Children classes, later I want to add more stuff into the Father class without modifying the original Father class, please teach me how could I do it.

Let me clarify my question. Any valuables and methods in Father will automatically accessible to all the Children classes, right? For example:

public class Father
{
    public int a;
    public int b;
    public int c()
    {
        return a+b;
    }
}

public class Child : Father
{
    private int d=a*b+c();
}

Now I want to add a boolean e and a method f() to Father so my Child could access it directly, without adding those lines directly in Father or make a new class in between Father and Child. Is that possible? How could I do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You could use [partial classes](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods). You are still modifying the original Parent class, but in a separate file. You could also use [extension methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) to add extra functionality to your Parent class without modifying the original class. – Swimburger Nov 14 '17 at 10:23
  • You are correct that you will need to add partial which modifies the original class, though from that point on it is possible. Extension methods can be applied to interfaces and classes. In the future you will even be able to add Extension properties. So extension methods should do the job. Although the purpose isn't clear. I think you'll have to figure out a better OOP design to accomplish your goal. – Swimburger Nov 14 '17 at 10:59
  • If I remember right, for the partial classes to work, the "original class" has to be partial too! What if it's NOT the case!? And if I use the extension methods, it would be just like me creating a new class. It defeat the purpose here! Is there any way other than these two which will do the trick!? Thank you very much! –  Nov 14 '17 at 11:11
  • Whatever you are trying to achieve, the additional code will need to live somewhere, whether it's in a static class, partial class, ... Maybe if you make it more concrete, we can help you. What you're asking doesn't make much sense right now. – Swimburger Nov 14 '17 at 11:22
  • OK, I apologize! The question came up when I'm working on a project using Visual C#. There are certain valuables I wish to be accessible across ALL forms! I use the "in-between inheritance" method like Anastasios Selmanis suggested below, but finding changing the inherited class from ALL the forms tedious! And I don't want to go modify the original Form class because I don't think those valuables will be used in other projects! So I was wondering if there is any way to compensate. There might be a way to make those valuables global, but I think finding out the proper way to extend the base ... –  Nov 14 '17 at 11:53
  • class would be MUCH MUCH MORE useful in the future!!! Thank you very very much for answering!!! –  Nov 14 '17 at 11:54
  • No problem. I think we've all created a BaseController to share functionality across multiple inheriting controllers easily. This works great, though be careful that you're not creating a 'God-class'. A class that contains a huge amount of functionality. I would propose that you use a combination of extension methods + try spreading out the functionality over multiple independent classes without the use of inheritance. Favor composition over inheritance. – Swimburger Nov 14 '17 at 12:35
  • Wouldn't that be similar to "in-between inheritance method"!? –  Nov 16 '17 at 03:01
  • That would be similar, or exactly the same as in between inheritance. It may not satisfy what you want, but I think those are your options. – Swimburger Nov 16 '17 at 14:22
  • OK... thanks a lot!!! –  Nov 21 '17 at 12:25

2 Answers2

2

You are looking for Extension Methods. Refer this post for more information.

mrtyormaa
  • 862
  • 1
  • 7
  • 17
2

If you want to add more fields to your father model then maybe your inheritance structure should be changed.

For example you had model A and model B. Model B inherited fields from model A

public class A
{
    public string FieldA { get; set; }
}

public class B : A
{
    public string FieldB { get; set; }
}

This way the class B has fields FieldA and FieldB.

Now if you want your class B to inherit from a class that has the fields of A but more you could add another layer to your inheritance tree like this.

public class A
{
    public string FieldA { get; set; }
}

public class C : A
{
    public string FieldC { get; set; }
}

public class B : C
{
    public string FieldB { get; set; }
}

Now your class B has fields FieldA and FieldB and FieldC and you don't have to alter your original father class A.

Now if you want to extend methods for a class you could also go with static extensions like this:

Public static void DoSomething(this A a)
{
    ... 
} 

and then call A.DoSomething();

Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
  • How about I don't want to create an in-between class!? For reasons like both A and B are out of my accessibility!? All I could hope to do is to extend A so it could be run without my interference. Is it possible!? Thanks!!! –  Nov 14 '17 at 10:31