Re: Access outer class members from a nested class

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



Matthew Ireland wrote:
I would like to access the members of an outer class from methods in an inner class. I know the following works, but I was hoping for something a little more elegant. For example, the JAVA syntax of Outer.this.m_OuterValue does not exist apparently.

Java has two different kinds of nested classes. It's important to understand Java, before one makes the attempt to apply Java concepts to C#.

The type of nested class you appear to be thinking of is an "inner class". An inner class has an implicit reference to an instance of the outer class, determined at the time of instantiation, based on the instance of the outer class that created the inner class.

Obviously, this means that when creating an inner class in Java, you must always do it via an instance of the outer class. It also then provides the implicit ability to always access members of the outer class, but specifically in the instance of the outer class that created the inner class.

The other kind of nested class in Java is the "static nested class". This type of nested class must be declared with the keyword "static", which in that context has a completely different meaning from the use of the word "static" elsewhere. That is, the class itself can still be instantiable; it just means that it has no implicit reference to an instance of the outer class.

Now, what does this all mean for C#? Well, C# doesn't have the concept of an "inner class". All nested classes are like Java's "static nested class". This has a couple of consequences: 1) you can have nested classes that are actually "static" in the same sense of "static" for top-level classes -- that is, they contain only static members, and 2) while the nested class has the same accessibility to members of the containing class that applies in Java, you must always provide an explicit reference to an instance of that containing class in order to access instance members of the containing class.

So, the code example you showed is the only approach in C# you can use.

By the way, please note that "elegant" is in the eye of the beholder. The Java approach adds an unnecessary complexity and inefficiency to the language, by overloading the "static" keyword, and by making the default for nested classes carry around a reference to the containing class, as well as obfuscating the relationship between the code that is written and what effect it has (because outer class members can be accessed implicitly).

Pete
.



Relevant Pages

  • Re: Inner Classes a liability
    ... For many of us who've been around since Java 1.0.2 (and ... It's good to get the difference between overriding and overloading down; ... For outer class methods, I'd wonder if use of the word ... You should think of the ability of an inner class to call methods of its ...
    (comp.lang.java.programmer)
  • Re: Trying to create a working internal confirm JOptionPane
    ... inner class, because it is an inner class, belongs to an object of the outer ... That means there is an outer class "this", ... reference doesn't make it one. ... You are talking about a core Java concept, the keyword "this", the ...
    (comp.lang.java.help)
  • Re: static classes
    ... The only difference between static nested and inner classes in Java is that an inner class has an implicit reference to an instance of the outer class. ... public int getOuterI ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Access outer class members from a nested class
    ... outer class says you can't access non-static members. ... less explicit than the Java approach. ... light of the nested class behaving as a static member, ...
    (microsoft.public.dotnet.languages.csharp)
  • Inner classes use
    ... don't see anyone outside of the enclosing class wanting to instantiate ... Second, when you make an inner class (i.e., ... > the outer class (although this may happen in the case of a nested class ...
    (comp.lang.java)