Re: Access outer class members from a nested class
- From: "Matthew Ireland" <mireland@xxxxxxxxxxxxx>
- Date: Wed, 14 Oct 2009 17:26:18 -0400
Many thanks for the explanation. That makes sense...
Though I think the paradigm for "static nested class" is the same as a
static method or variable, i.e."you don't need an instance to access it".
So in that light "static" is not really overloaded. The oddity is when you
think of "static" as meaning "there is only one instance shared by all class
instances". So "static" itself is a little bi-polar to begin with.
But then the error you get from the C# compiler when you try to access the
outer class says you can't access non-static members. So the C# syntax has
an implicit "static" attached to the behavior of nested classes, which is
less explicit than the Java approach. This is definately a simplifying
assumption (and we all know about assumptions). I guess I was thinking that
passing a reference to the parent object was a bit brute force. But in the
light of the nested class behaving as a static member, it is the only way.
The problem with either approach is the violation of the second definition
of "static". As a class you want to be able to make independent instances
of the class, but as a "static" member you only want one instance to be
shared.
Oh well...
All development is a compromise.
Thanks again.
"Peter Duniho" <no.peted.spam@xxxxxxxxxxxxxxxxxx> wrote in message
news:%23TXxy1QTKHA.4360@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: Access outer class members from a nested class
- From: J.B. Moreno
- Re: Access outer class members from a nested class
- From: Peter Duniho
- Re: Access outer class members from a nested class
- References:
- Access outer class members from a nested class
- From: Matthew Ireland
- Re: Access outer class members from a nested class
- From: Peter Duniho
- Access outer class members from a nested class
- Prev by Date: Re: Difference between nullable class and nullable<> structure
- Next by Date: Re: At least one space ...
- Previous by thread: Re: Access outer class members from a nested class
- Next by thread: Re: Access outer class members from a nested class
- Index(es):
Relevant Pages
|