1

and sorry in advance if this question has been solved previously.

I am creating a small library in C# and am hoping to have class A be able to modify the data members of class B, where class A and B exist in the same namespace, C.D. This is not a problem to accomplish though I would like class E, in namespace C, to not be able to access the data members of B.

namespace C.D
{
    class B
    {
        modifier int
            a,
            b;

        public B()
        {
        }
    }

    class A
    {
        public A() {}

        public B DoStuff()
        {
            B b = new B();
            b.a = 1; b.b = 2;
            return b;
        }
    }
}

namespace C
{
    class E
    {
        static void Main(String[] args)
        {
            A a = new A();
            B b = a.DoStuff();
        }
    }
}

In my main method above I would like every class in the namespace C.D to be able to alter the data members of a class B object though nothing outside of the C.D namespace to be able to modify class B object data members.

Is there any way to do this by changing the namespace structure, modifiers, or implementing specific design patterns?

Thank you all in advance. : )

Dawson
  • 4,391
  • 2
  • 24
  • 33

2 Answers2

2

You can use the "internal" access modifier if you're willing to move the relevant classes in C.D to a separate assembly.

namespace C.D
{
    class B
    {
        internal int a;
        internal int b;

        public B()
        {
        }
    }
}
Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
  • Thank you very much. I read over the internal modifier though was hoping there would be a different way to implement a solution to my problem. I guess I just have to not be lazy : ) – Dawson Aug 29 '12 at 19:47
  • You're welcome. There are other ways around this. For example, you could create an interface for class B that exposes the necessary properties/methods. The classes that need to fiddle with internal details would be provided with the concrete implementation (or another interface) somehow. But this sort of restructuring is obviously a _lot_ more work. – Peter Ruderman Aug 29 '12 at 20:07
0

One way to achieve this is by:

  1. Moving Class A's definition into Class B (nesting it within class B { ... })

  2. Making Class B's fields (a and b) private.

i.e.

namespace C.D
{
    class B
    {
        private int a, b;

        public B()
        {
        }

        class A
        {
            public A() { }

            public B DoStuff()
            {
                B b = new B();
                b.a = 1; b.b = 2;
                return b;
            }
        }
    }
}

namespace C
{
    class E
    {
        static void Main(String[] args)
        {
            A a = new A();
            B b = a.DoStuff();
        }
    }
}

Another way to achieve this is to make Class B's fields protected, then make all classes that should have access to Class B's fields derive from Class B. This way you won't need to nest Class A inside Class B. i.e.

namespace C.D
{
    class B
    {
        protected int a, b;

        public B()
        {
        }
    }

    class A : B
    {
        public A() {}

        public B DoStuff()
        {
            B b = new B();
            b.a = 1; b.b = 2;
            return b;
        }
    }
}

namespace C
{
    class E
    {
        static void Main(String[] args)
        {
            A a = new A();
            B b = a.DoStuff();
        }
    }
}
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76