Access from inner classes

  • Thread starter Thread starter Marco
  • Start date Start date
M

Marco

Howdy!
Given:

public abstract class A {

public abstract int A1(int i);

private class B {
private int B1(int i) {
int j;
...
----------> j = A1(4); // that's what I want
...
}

}

Question: How do I reference the abstract method A1 from
class A inside the method B1 from the inner class B ?

Thanx.

______
Marco
 
Marco said:
Howdy!
Given:

public abstract class A {

public abstract int A1(int i);

private class B {
private int B1(int i) {
int j;
...
----------> j = A1(4); // that's what I want
...
}

}

Question: How do I reference the abstract method A1 from
class A inside the method B1 from the inner class B ?

There are no inner classes in C#, only nested classes - the equivalent
of nested classes marked as "static" in Java. To call A1, you'd need an
instance of A (well, an instance of a subclass of A, in this case). How
you get that is up to you.
 
Well, here's a new concept for me. What's the point of this generally? I
can see maybe to severely limit the visibility of ClassB even within the
assembly, but is that it?

public class ClassA
{
private class ClassB
{
}
}
 
Daniel Billingsley said:
Well, here's a new concept for me. What's the point of this generally? I
can see maybe to severely limit the visibility of ClassB even within the
assembly, but is that it?

public class ClassA
{
private class ClassB
{
}
}

Basically, yes. If one class is only ever going to be used from within
another, it keeps it nice and tidily away from everything else.
 
Ok, I meant nested class.
But the point is: To reference a object of a class inside
of itseld I use "this" but for a nested class what should I
use ? "this.this" ???? :-)

______
Marco
 
Marco said:
Ok, I meant nested class.
But the point is: To reference a object of a class inside
of itseld I use "this" but for a nested class what should I
use ? "this.this" ???? :-)

There *is* no this - there's no implicit instance as there is with an
inner class in Java. You can have an instance of the nested class
without there being *any* instances of the outer class at all.
 
Here is an exerpt from MSDN article...

Nested classes are useful when an object will logically contain subordinate
objects, but other objects would have use for those objects. An example
might be a Wheel class. This could be a class that clients could create and
use wherever a wheel might be needed in their application. But except in
the most primitive implementations, a wheel is not just a single object,
but is composed of several subordinate objects, each of which build the
wheel. A wheel might have a Rim object, Tire object, Spoke objects and
other objects, without which the wheel could not function. But the average
user would have no need to create a spoke, or a rim, or a bearing — all
he's interested in is the wheel.

In a case like this, it makes sense for the Wheel class to contain the
implementation for all of its subordinate classes. This way, the wheel can
create and manage any contained objects it may need while conveniently
hiding the details of this implementation from the client. Those
subordinate objects the client might reasonably need to have contact with
now and again (for example, a Tire object) can be exposed as part of the
public object model, and those that a client should never see (for example,
a Bearings collection) could be declared private and hidden.


--------------------
From: "Daniel Billingsley" <[email protected]>
References: <[email protected]>
Subject: Re: Access from inner classes
Date: Mon, 10 Nov 2003 09:14:04 -0500
Lines: 25
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 68-74-16-211.ded.ameritech.net 68.74.16.211
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198029
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Well, here's a new concept for me. What's the point of this generally? I
can see maybe to severely limit the visibility of ClassB even within the
assembly, but is that it?

public class ClassA
{
private class ClassB
{
}
}

Jon Skeet said:
There are no inner classes in C#, only nested classes - the equivalent
of nested classes marked as "static" in Java. To call A1, you'd need an
instance of A (well, an instance of a subclass of A, in this case). How
you get that is up to you.


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top