"ChrisB" ...
An object that is a field in another object has a constructor
that requires a reference to the containing object:
// object fields
ChildObject childObject = new ChildObject(this);
When attempting to compile this code, a message is returned
that states that the "this" keyword is not available in this
context.
As you haven't *shown* the context for that statement, I can only guess, but
it's most likely that the statement is made within a *static* method.
Static methods doesn't belong to any instance, but to the class itself,
hence there is no "this" in taht context.
Any way around this?
Either make the context "non-static", and make further adjustments to the
rest of the code to deal with that, e.g.
class Mother
{
public void NonStaticMethod()
{
ChildObject childObject = new ChildObject(this);
}
// ...
}
or use another reference to the instance of the containing object, e.g.
class Mother
{
public static void StaticMethod(Mother m)
{
ChildObject childObject = new ChildObject(m);
}
// ...
}
--- another example ---
class Mother
{
public static void Main()
{
Mother m = new Mother();
ChildObject childObject = new ChildObject(m);
}
// ...
}
...etc. Which solution is best for you is depending on what you really want
to do.
HTH.
// Bjorn A