1

I got a C# project where I need to have a base class and two sub classes and I want the sub classes to be private to the base classes. This is how it looks like:

public class MyBaseClass
{
    public void doMyWork()
    {
        doSubClass1();
        doSubClass2();
    }
}

private class MySubClass1
{
    public void doSubClass1()
    {
        //Do stuff
    }
}

private class MySubClass2
{
    public void doSubClass2()
    {
        //Do stuff
    }
}

How i can i get this to work?

Abbas
  • 14,186
  • 6
  • 41
  • 72
Rene Nielsen
  • 87
  • 1
  • 7
  • 5
    I don't understand what you need. Please rephrase your question – Only a Curious Mind Feb 24 '14 at 13:42
  • 1
    Sorry, but the code as presented does not even compile, because there is no way that `MybaseClass` can "see" the methods in the other classes (which are not sub classes at the moment). What exactly do you want to accomplish? – John Willemse Feb 24 '14 at 13:43
  • 1
    The only way to do this as I see it, would be public class MyBaseClass { private class MySubClass1 {} private class MySubClass2 {} } This would be encapsulating those two classes in your base class. However please take notice that this is really bad design! You should probably rethink your architecture to avoid this situation. – woutervs Feb 24 '14 at 13:46
  • So, encapsulation? If that is what you are trying to accomplish.. – Andrew Backes Feb 24 '14 at 13:46
  • the thing is that i want the classes in 3 different .cs files – Rene Nielsen Feb 24 '14 at 13:53
  • 1
    Is this what you want? [Partial Classes](http://msdn.microsoft.com/en-us/library/wa80x488.aspx) – nmclean Feb 24 '14 at 14:04

2 Answers2

3

Does it hurt if the classes themselves are not-so-private but still wrapped in your base & unaccessible outside?

public class MyBaseClass
{
    private readonly MySubClass1 _sub1 = new MySubClass1();
    private readonly MySubClass2 _sub2 = new MySubClass2();

    public void DoMyWork()
    {
        _sub1.DoSubClass1();
        _sub2.DoSubClass2();
    }
}

public class MySubClass1
{
    public void DoSubClass1() { /* Do stuff */ }
}

public class MySubClass2
{
    public void DoSubClass2() { /* Do stuff */ }
}

If this is not good enough and you need those in separate files, as you stated, then you can use partial classes, like so.

Class1.cs file

public partial class MyBaseClass
{
    public void DoMyWork()
    {
        (new MySubClass1()).DoSubClass1();
        (new MySubClass2()).DoSubClass2();
    }
}

Class2.cs file

public partial class MyBaseClass
{
    private class MySubClass1
    {
        public void DoSubClass1() { /* Do stuff */ }
    }
}

Class3.cs file

public partial class MyBaseClass
{
    private class MySubClass2
    {
        public void DoSubClass2() { /* Do stuff */ }
    }
}   

But still it's not quite clear what you are trying to achieve here.

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
2

If you put your subclasses inside of the base class, the base class can access them. To use your example, it would be like this:

public class MyBaseClass
{
    public void doMyWork()
    {
        new MySubClass1().doSubClass1();
        new MySubClass2().doSubClass2();
    }

    private class MySubClass1
    {
        public void doSubClass1()
        {
            //Do stuff
        }
    }

    private class MySubClass2
    {
        public void doSubClass2()
        {
            //Do stuff
        }
    }

}

Note that all I did was move the last } to the bottom.

Gabe
  • 84,912
  • 12
  • 139
  • 238