"this" keyword not available

  • Thread starter Thread starter ChrisB
  • Start date Start date
C

ChrisB

Hello:

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.

Any way around this?

Thanks,
Chris
 
Any way around this?

Initialize it in the constructor instead.

class Foo
{
ChildObject childObject;

Foo()
{
childObject = new ChildObject(this);
}
}


Mattias
 
"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
 
Without seeing more code, it would be difficult to say exactly.

But the general idea is that the "this" keyword refers to the current
instance of the parent type of ChildObject.

If there isn't yet an instance (e.g. the ctor hasn't been run, or the code
you refer to isn't in the ctor, that's why the reference to "this" fails at
compile time.

In other words, there has to be an instance of the class that "this" refers
to before you can use it from within that class.
Peter
 
Why is 'this' not available? Are you calling it from a static function?
Technically you could create a new instance whatever 'this' is pointing to,
though it may yield unexpected results.
Cheers,
Mark.
 
ChrisB said:
Hello:

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.

Any way around this?

Thanks,
Chris
"this" is only available in non-static methods, as well as constructors.

It is not available in static methods, nor in the context you have it
where you define members of the class.

Ie. this is not possible:

public class SomeClass
{
ChildObject childObject = new ChildObject(this);

public SomeClass()
{
...
}
}

instead, do this:

public class SomeClass
{
ChildObject childObject;

public SomeClass()
{
childObject = new ChildObject(this);
...
}
}
 
Back
Top