473,396 Members | 1,913 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,396 software developers and data experts.

NOOB QUESTION: How can I access an element in nested classes

Here is a sample to explain my problem
class Foo
{
int a;
class Bar
{
void ChangeA()
{
a = 1;
}
}
}
function ChangeA() does not work because 'a' doesn't belong to Bar; it
belongs to Foo. So the question is: how can I make a function inside
class Bar change an element that belongs to Foo?

This could be possible cause(in my program) each object of kind Foo
has inside it at least one object of kind Bar and this object have to
change his 'parent' object.

Oct 23 '07 #1
2 937
"hstagni" <st****@gmail.comwrote in message
news:11*********************@t8g2000prg.googlegrou ps.com...
Here is a sample to explain my problem
class Foo
{
int a;
class Bar
{
void ChangeA()
{
a = 1;
}
}
}
function ChangeA() does not work because 'a' doesn't belong to Bar; it
belongs to Foo. So the question is: how can I make a function inside
class Bar change an element that belongs to Foo?
There is no direct way to do that. Even if the class is nested, it doesn't
see the contents of the surrounding class. One possible solution is to pass
a reference to the container into the contained class when instancing the
latter:

class Foo
{
public int a;

public void DoSomethingWithBar()
{
Bar x = new Bar(this);
x.ChangeA();
}

class Bar
{
private Foo container;
public Bar(Foo container)
{
this.container=container;
}
public void ChangeA()
{
container.a = 1;
}
}
}

Oct 23 '07 #2
The problem here isn't so much that one thing can't see the other as
that you're trying to change instance members in class definitions.
You create a class definition that defines int a, then a class that
tries to change the instance of int a. The class definition of Foo
doesn't actually contain any int a, just the notion that an object of
the type defined in Foo would have an int a. You'll notice that Mr.
Poblacion's response clearly defines this distinction by instantiating
classes.

Mr. Poblacion's solution should work perfectly for many needs.
Another, one that would serve other needs, would be to define Foo.a as
a static int, in which case Foo.Bar.ChangeA() could modify Foo.a. In
that case, Foo myFoo would contain no definition int a, i.e., there
would be no myFoo.a, just Foo.a.

s}
On Oct 23, 9:41 am, hstagni <sta...@gmail.comwrote:
Here is a sample to explain my problem

class Foo
{
int a;
class Bar
{
void ChangeA()
{
a = 1;
}
}

}

function ChangeA() does not work because 'a' doesn't belong to Bar; it
belongs to Foo. So the question is: how can I make a function inside
class Bar change an element that belongs to Foo?

This could be possible cause(in my program) each object of kind Foo
has inside it at least one object of kind Bar and this object have to
change his 'parent' object.

Oct 23 '07 #3

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

Similar topics

14
by: theo | last post by:
if I have nested div combinations, can I call for styles only to specific nested combos? It's 3 lists <li>, on one page, needing different styles. <div id=list1><li> <a id="t1"...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
25
by: Brian | last post by:
Can some one please tell me what I'm doing wrong. I'm trying to create a class called Dog, but Visual Basic tells me that I can't enter Wolf.age....why is this? Public Class Form1 Public Class...
8
by: azz131 | last post by:
Hi, i want to access an array of objects inside a method like this using System; using System.Collections.Generic; namespace ObjectArray { class MainClass{ class MyClass
1
by: nuffnough | last post by:
I have defined two classes with one common field (called code) and several different fields. In class A there is only one instance of any given code as all items are individual. In class B, there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.