Problem passing 'this'

G

Guest

I have a data model with this structure:
Class
PropertyA
PropertyB
Collection
Class
Property1
Property2

The outermost class has a handler that correctly deals with events fired by
properties within it (ie. PropertyA, PropertyB, etc.).

The innermost class has a similar handler for dealing with its own properties.

What I want to do is have the innermost handler pass its data to the
outermost handler. But I can't seem to accomplish this and am not sure why.
Here's the code that I thought would work:

public class _Question // outermost class
{
private _Choices instanceChoices = new _Choices(this);
public _Choices Choices
{
get
{
return instanceChoices;
}
}
}


The "_Choices" constructor is setup to receive an instance of "_Choice"
being passed to it but the compiler rejects the "this" that you see above.
It reports this error: "Keyword this is not available in the current context"

But I don't understand why. Can someone explain?
 
T

Tom Porterfield

I have a data model with this structure:
Class
PropertyA
PropertyB
Collection
Class
Property1
Property2

The outermost class has a handler that correctly deals with events fired by
properties within it (ie. PropertyA, PropertyB, etc.).

The innermost class has a similar handler for dealing with its own properties.

What I want to do is have the innermost handler pass its data to the
outermost handler. But I can't seem to accomplish this and am not sure why.
Here's the code that I thought would work:

public class _Question // outermost class
{
private _Choices instanceChoices = new _Choices(this);
public _Choices Choices
{
get
{
return instanceChoices;
}
}
}

The "_Choices" constructor is setup to receive an instance of "_Choice"
being passed to it but the compiler rejects the "this" that you see above.
It reports this error: "Keyword this is not available in the current context"

But I don't understand why. Can someone explain?

Is not this an instance of _Question in our example as instanceChoices is a
private instance member of an instance of _Question. It appears you are
not passing the correct type of object to the _Choices constructor.
 
G

Guest

Hi Tom,

The instantiated "Choices" you see in the original code I posted was simply
a read-only property within the "_Question" class.

Here's the beginning of the actual "_Choices" class:

public class _Choices : CollectionBase
{
private _Question question;
public _Choices(_Question question)
{
this.question = question;
}



You see that I indeed have specified the contructor's single parameter to be
of type "_Question". Yet I can't seem to pass an instance of "_Question" to
it via "this".

This is what I don't understand,

Robert W.
 
E

Ennixo

hi, you should change the following line :
private _Choices instanceChoices = new _Choices(this);
to
private _Choices instanceChoices;

and add :
instanceChoices = new _Choices(this);
inside the _Question's constructor.

it should work.


Robert W. a écrit :
 
G

Guest

Ennixo,

Since posting this, I did find a solution, but your way is much more OOP and
elegant. Thank you!

You know, being new to C# and OOP, after I see a great solution like this,
it seems "so obvious". But at the time I'm struggling with the problem . . .
ughhhhh!!!

Thank you again!!

Robert W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top