By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

Access from inner classes

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Marco <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3

P: n/a
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
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
Nov 15 '05 #5

P: n/a
Marco <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
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" <db**********@NO.durcon.SPAAMM.com>
References: <07****************************@phx.gbl> <MP************************@msnews.microsoft.com >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: <ux**************@TK2MSFTNGP12.phx.gbl>
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!TK2MSFTN GP12.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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.