none
is it possible to make an a child class just to simplify the parent RRS feed

  • Question

  • i have a big class, containing many fields and many constructors(over loaded and others) and a lot of methods (to help parsing data,..),..and i wanna to minimizing the size of the class (make it simple)can i just make a child class containing only methods and constructors (ex: parsing methods ..)
    • Edited by essamce Sunday, January 20, 2019 9:39 PM
    Sunday, January 20, 2019 9:39 PM

Answers

  • Greetings essamce.

    What you are describing is possible, and some developers do just that. The usual structure is to have the data in one class and the methods in a class with the name suffix "Utils" or "Utilities".

    public class MyClass
    {
       public int SomeData{get; set;}
    }
    
    public class MyClassUtilities
    {
       public int static GetDataSquared(MyClass mc)
       {
          return mc.SomeData * mc.SomeData;
       }
    }
    
    
    // Use like so.
    MyClass mc = new MyClass();
    mc.SomeData = 2;
    int square = MyClassUtilities.GetDataSquared(mc);

    Personally, I hate this approach. You have to know the utilities class exists and where to find it, rather than just having everything together in one place. But some people insist on using it.

    • Proposed as answer by Stanly Fan Thursday, January 24, 2019 2:05 AM
    • Marked as answer by essamce Wednesday, February 27, 2019 9:27 AM
    Sunday, January 20, 2019 10:25 PM

All replies

  • You can use several partial classes/class files that can be combined into one class.

    http://www.tutorialsteacher.com/csharp/csharp-partial-class

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods


    • Edited by DA924x Monday, January 21, 2019 4:03 AM
    Sunday, January 20, 2019 10:25 PM
  • Greetings essamce.

    What you are describing is possible, and some developers do just that. The usual structure is to have the data in one class and the methods in a class with the name suffix "Utils" or "Utilities".

    public class MyClass
    {
       public int SomeData{get; set;}
    }
    
    public class MyClassUtilities
    {
       public int static GetDataSquared(MyClass mc)
       {
          return mc.SomeData * mc.SomeData;
       }
    }
    
    
    // Use like so.
    MyClass mc = new MyClass();
    mc.SomeData = 2;
    int square = MyClassUtilities.GetDataSquared(mc);

    Personally, I hate this approach. You have to know the utilities class exists and where to find it, rather than just having everything together in one place. But some people insist on using it.

    • Proposed as answer by Stanly Fan Thursday, January 24, 2019 2:05 AM
    • Marked as answer by essamce Wednesday, February 27, 2019 9:27 AM
    Sunday, January 20, 2019 10:25 PM