Re: NOOB QUESTION: How can I access an element in nested classes
- From: "Alberto Poblacion" <earthling-quitaestoparacontestar@xxxxxxxxxxxxx>
- Date: Tue, 23 Oct 2007 18:15:06 +0200
"hstagni" <stagni@xxxxxxxxx> wrote in message news:1193154118.609622.91980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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;
}
}
}
.
- References:
- Prev by Date: Re: Creating a dataset?
- Next by Date: Re: Re-Connect with Remote SQL Servr 2005 Database
- Previous by thread: NOOB QUESTION: How can I access an element in nested classes
- Next by thread: Re: NOOB QUESTION: How can I access an element in nested classes
- Index(es):
Relevant Pages
|