473,794 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does a nested class reference it's containing class.

I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #1
12 4045
base is what you want:

int intBigClassID = base.ID_BigClas s;

Mortos wrote:

I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #2
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClas s;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)
Nov 15 '05 #3

There's no automatic relation between a BigClass and a MiniClass
instance. You have to pass in a reference to the BigClass if you need
one.

public MiniClass{
public void MiniFunc(BigCla ss big){
int intBigClassID = big.ID_BigClass ;
}
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4
Mortos wrote:
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClas s;

{Snip}


There is no relationship between a nested type and its "container" ,
other than scoping. The creating of an instance of the nested type does
not imply any relationship at all to any particular instance of the
"containing " type. In this instance, "containing " applies _only_ to the
source code, not to any actual usage of the types.

If the nested type needs a reference to the type it was defined inside
of, then the outer type will need to pass a reference to itself to the
nested type's constructor and/or a property on the nested type.
Nov 15 '05 #5
Hi Mortos,

There is not a hardwired relationship between both classes, you can create
either one independently from the other like this:

BigClass.MiniCl ass instance = new BigClass.MiniCl ass()

BigClass instance = new BigClass()
Creating a BigClass instance does not create a MiniClass. I think that there
are two big differences between declaring both class at the same level or
one inside other:
1- Naming polution control, you decrease the number of types on the
namespace, as you will reference the inner class as ExternalClass.I nnerClass
.. This also give an idea that the InnerClass is related to the OuterClass.

2- The inner class has access to the private members of the outer class, so
you can do this :
public class BigClass
{
private int ID_BigClass = -99;

public class MiniClass
{
public void MiniFunc(BigCla ss b)
{
int intBigClassID = b.ID_BigClass;
}
}

}

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mortos" <th*****@spamme r.co.uk> wrote in message
news:_h******** *********@news-binary.blueyond er.co.uk...
I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #6
base won't work, you are right Mortos. But how could this work with
non-statics anyway -- which outer class instance are you referring to?

Interestingly, the MS compiler was smart enough to tell what I was trying to
do:

C:\projects\tes ting\wintest1\W inTest1.cs(135) : Cannot access a nonstatic
member of outer type 'wintest1.frmWi nTest1.Outer' via nested type
'wintest1.frmWi nTest1.Outer.In ner'

Indeed, making it static works:

public class Outer
{
public *static* int f;

public class Inner
{
public int g;

public Inner()
{
g = f;
}
}
}

"Mortos" <th*****@spamme r.co.uk> wrote in message
news:pQ******** ********@news-binary.blueyond er.co.uk...
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClas s;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #7
Mortos <th*****@spamme r.co.uk> wrote:
I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.


That's because the Java idea of an inner class with an implicit
reference doesn't exist in C#. Nested classes in C# are (mostly) like
static nested classes in Java.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Brad Williams wrote:
C:\projects\tes ting\wintest1\W inTest1.cs(135) : Cannot access a nonstatic
member of outer type 'wintest1.frmWi nTest1.Outer' via nested type
'wintest1.frmWi nTest1.Outer.In ner'

Indeed, making it static works:


No, it makes it _compile_. It does not solve the problem posted by the
original poster. The behavior in this case is very different.
Nov 15 '05 #9
Mortos wrote:

I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClas s;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)


Duh -- yeah, my mistake.
Nov 15 '05 #10

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

Similar topics

6
2570
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
4
365
by: Sundararajan | last post by:
Dear Folk, I have implemented a nested class as follows: Class Enclosing { string strName; Class Nested1 { }
9
3689
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here? End Sub
1
2578
by: Craig Buchanan | last post by:
is there an implicitly-defined Parent property defined for a class that is nested in another class? i'm looking to simplify my coding. At this point, i've defined the class as so: Class Item Private _SubItem As New SubItem(Me) 'other properties and methods
1
1648
by: Diane Yocom | last post by:
I have two nested master pages, each with public properties. From my content page, I am able to access properties from both pages using either Master.PropertyName (for the child master page) or Master.Master.PropertyName (for the parent master page). The solution compiles and the HTML looks right, but my code has blue "error" underlines wherever I type Master.Master.PropertyName. When I hover over the underlined code, I get the...
2
2374
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
1
11816
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an XmlNodeList. I am trying to deserialize the nodes using a foreach as follows: foreach(XmlNode lineItem in LineItemsNodeList) An abbreviated example of the nested lineItem node looks like this:
1
5330
by: =?Utf-8?B?QmFybmV5TGlnaHQ=?= | last post by:
Hi, I have a WCF service contract which has message containing a List<MyType> data member where MyType consists of a string Key and object Value. As expected this gets exposed as a MyType array in the service reference. In the client, I dynamically add Key/Value entries in the list. This works fine as long as I use simple types in the client, but I need to
3
3899
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.