Re: "Private" nested classes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mikolas (Mikolas_at_discussions.microsoft.com)
Date: 07/29/04


Date: Thu, 29 Jul 2004 06:08:01 -0700

The perfect solution would probably be making the constructor of the internal class visible only to the outter class. I'm not aware of a technique that would enable that. Or in c++ there's the friend keyword, which hasn't been adopted into C#(as far as I now)
Wouldn't making the inner constructor private solved the problem though?

"Etienne Boucher" wrote:

> The goal is to still be able to access the members of the nested type from
> outside the outter class. From the definition
>
> public class Outter
> {
> private Inner i;
>
> public Outter()
> {
> new Inner(this);
> }
>
> public Inner InnerAccessor
> {
> get { return i; }
> }
>
> public class Inner
> {
> private Outter o;
> public int MemberInt;
>
> public Inner(Outter o)
> {
> if (null != o.i)
> throw new Exception();
> o.i = this;
> this.o = o;
> }
> }
> }
>
> The following code can still be exectuted.
>
> Outter o = new Outter();
> Console.WriteLine(o.InnerAccessor.MemberInt);
>
> Again, if anyone knows a better way to do this.
>
> Etienne Boucher
>
>
>



Relevant Pages

  • Listeners Blocking Each Other And Needing Events To Trickle Down
    ... I am writing a program with a JPanel inside of another JPanel. ... However, when a MouseListener is added to the inner JPanel, ... the outter JPanel's MouseMotionListener is no longer triggered when the ...
    (comp.lang.java.gui)
  • Simple way to restore object relationships after deserialization?
    ... I'm working with XML serialization of hierarchical objects such as the ... public class OuterClass ... public InnerClass Inner ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sinus Infection from Scuba
    ... I meant inner as opposed to outter. ... I suspect we are agreed. ... Not to expect that any remedy might be an instant solution. ...
    (rec.scuba)
  • Protected inner classes and inheritance
    ... a top-level class with a protected inner class. ... I have a second-level class ... in a different package that extends the top-level class. ... public class SecondLevel extends TopLevel ...
    (comp.lang.java.programmer)
  • "Private" nested classes
    ... Nested classes are usualy objects made to only live in their parent object ... public class Outter ... still being able to access the members of Inner. ...
    (microsoft.public.dotnet.languages.csharp)