© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
23 replies
Gipper

Best way to do this OOP-wise???

Ok, so this is not the actual program but for the sake of brevity I'll give you an equivalent one:

namespace namespace1
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            BaseClass baseClass = new BaseClass();
            DerivedClass1 = new DerivedClass1();//Here I initialize fields in BaseClass
            DerivedClass2 = new DerivedClass2();//Here I want the data initialized in BaseClass on the previous line
        }
    }
}
namespace namespace1
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            BaseClass baseClass = new BaseClass();
            DerivedClass1 = new DerivedClass1();//Here I initialize fields in BaseClass
            DerivedClass2 = new DerivedClass2();//Here I want the data initialized in BaseClass on the previous line
        }
    }
}

internal class BaseClass
{
    private protected int someData;
    private protected string someMoreDataStr;
}
internal class BaseClass
{
    private protected int someData;
    private protected string someMoreDataStr;
}

internal class DerivedClass1 : BaseClass
{
    \\THIS CLASS INITIALIZES DATA TO THE FIELDS IN BASECLASS
    public DerivedClass1()
    {
        someData=3;
        someMoreDataStr = "foo";
    }
}
internal class DerivedClass1 : BaseClass
{
    \\THIS CLASS INITIALIZES DATA TO THE FIELDS IN BASECLASS
    public DerivedClass1()
    {
        someData=3;
        someMoreDataStr = "foo";
    }
}

internal class DerivedClass2 : BaseClass
{
    \\This Class will be instantiated after DerivedClass1 And I want to have the 3 And the "foo" In there
    public DerivedClass2()
    {
        \\I want to have the same data initialized earlier here
    }
}
internal class DerivedClass2 : BaseClass
{
    \\This Class will be instantiated after DerivedClass1 And I want to have the 3 And the "foo" In there
    public DerivedClass2()
    {
        \\I want to have the same data initialized earlier here
    }
}

Right now what I'm doing is:
namespace namespace1
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            BaseClass baseClass = new BaseClass();
            DerivedClass1 = new DerivedClass1(baseClass);//Here I initialize fields in BaseClass
            DerivedClass2 = new DerivedClass2(baseClass);//Here I want the data initialized in BaseClass on the previous line
        }
    }
}
namespace namespace1
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            BaseClass baseClass = new BaseClass();
            DerivedClass1 = new DerivedClass1(baseClass);//Here I initialize fields in BaseClass
            DerivedClass2 = new DerivedClass2(baseClass);//Here I want the data initialized in BaseClass on the previous line
        }
    }
}

and just accessing the data through the argument passed in to the constructor and I'm only inheriting so I can access the private protected field in BaseClass. Is there a better way of doing this (i.e. two classes derived from the same base sharing the values of the vars in the baseObj)? Should I just make the field public and not inherit?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

better way to do this?
C#CC# / help
3y ago
How to make this more oop firendly?
C#CC# / help
4w ago
✅ EF Best way to do UPDATE on entity
C#CC# / help
2y ago
Best way to do a discriminated union struct
C#CC# / help
4y ago