473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inner Class Extends Outer Class

A construction like this:

class Outer
{
class Inner:Outer
{
}
}

compiles without problem but does it introduce infinity?
Oct 28 '06 #1
5 2416
I've never thought of doing this! However, it seems to work OK.

public class Outer
{
public class Inner : Outer
{
public override string ToString()
{
return "I'm an inner";
}
}

public override string ToString()
{
return "I'm an outer";
}
}
To test it:

static void Main(string[] args)
{
Outer o = new Outer();
Console.WriteLine(o);

Outer.Inner i = new Outer.Inner();
Console.WriteLine(i);

Console.WriteLine("\nPress ENTER to continue");
Console.ReadLine();
}
Without using 'using' statements you have to refer to it as Outer.Inner
so it's a bit long winded! I'd prefer to declare them sequentially for
ease of readability but it seems that the compiler can cope with it OK.
Martijn Mulder wrote:
A construction like this:

class Outer
{
class Inner:Outer
{
}
}

compiles without problem but does it introduce infinity?
Oct 28 '06 #2
Hi Martjin,

Why would you think that it might "introduce infinity"?

After all, it's not like you are asking about something recursive, like this:

class Outer : Inner { }
class Inner : Outer { }

BTW, the above fails during compilation with an interesting message:

Error 2 Circular base class dependency involving 'Inner' and 'Outer'

--
Dave Sexton

"Martijn Mulder" <i@mwrote in message
news:45***********************@news.wanadoo.nl...
>A construction like this:

class Outer
{
class Inner:Outer
{
}
}

compiles without problem but does it introduce infinity?

Oct 28 '06 #3
Why would you think that it might "introduce infinity"?
>
After all, it's not like you are asking about something recursive, like
this:

class Outer : Inner { }
class Inner : Outer { }

BTW, the above fails during compilation with an interesting message:

Error 2 Circular base class dependency involving 'Inner' and 'Outer'


I can compile and run this program without problems. But I don't see any use
for it (yet):

//class Outer
public class Outer
{

//ToString
public override string ToString(){return "Outer class";}
//nested class Inner
public class Inner:Outer
{

//ToString
public override string ToString(){return "Inner class";}
}
//Main
static void Main()
{
System.Console.WriteLine(new Outer());
System.Console.WriteLine(new Outer.Inner());
System.Console.WriteLine(new Outer.Inner.Inner());
System.Console.WriteLine(new Outer.Inner.Inner.Inner());
}
}

_____________________
output:

Outer class
Inner class
Inner class
Inner class
Oct 28 '06 #4
Hi Martjin,

Yes, I suspect that it would work - I don't see why anyone would think that it
wouldn't. I don't know of any good use for it either ;)

--
Dave Sexton

"Martijn Mulder" <i@mwrote in message
news:45***********************@news.wanadoo.nl...
>Why would you think that it might "introduce infinity"?

After all, it's not like you are asking about something recursive, like
this:

class Outer : Inner { }
class Inner : Outer { }

BTW, the above fails during compilation with an interesting message:

Error 2 Circular base class dependency involving 'Inner' and 'Outer'

I can compile and run this program without problems. But I don't see any use
for it (yet):

//class Outer
public class Outer
{

//ToString
public override string ToString(){return "Outer class";}
//nested class Inner
public class Inner:Outer
{

//ToString
public override string ToString(){return "Inner class";}
}
//Main
static void Main()
{
System.Console.WriteLine(new Outer());
System.Console.WriteLine(new Outer.Inner());
System.Console.WriteLine(new Outer.Inner.Inner());
System.Console.WriteLine(new Outer.Inner.Inner.Inner());
}
}

_____________________
output:

Outer class
Inner class
Inner class
Inner class


Oct 28 '06 #5
Martijn Mulder <i@mwrote:

<snip>
I can compile and run this program without problems. But I don't see any use
for it (yet):
I may well use them when looking at "class enums". If you have a
private constructor in the outer class, it can be used from the nested
class, which means you can have a fixed, well-known set of derived
types. See the comments in
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx
for an example.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Murat Tasan | last post by:
i have an inner class and an outer class, both of which have an identically named member. how from the inner class can i reference the outer member? (the example code should illustrate this...
5
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to...
3
by: trialproduct2004 | last post by:
hi all i am having application which is using two classes. in one class i am creating instance of another class and calling method of that class. what i want is from inner class's method i want...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
4
by: amaca | last post by:
In this slightly contrived (though small, complete and perfectly formed) example: public class Inner { public event EventHandler InnerHandler; public void DoSomething() { if ( InnerHandler...
2
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
1
by: mrstephengross | last post by:
I'm making progress on mixing templates with friends (sounds like a drinking game, huh?). Anyway, here's the situation. I've got an "Outer" class with a private "Inner" class (sub-class,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
3
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.