471,306 Members | 835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

"this" keyword not available

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

Dec 7 '05 #1
5 2359
>Any way around this?

Initialize it in the constructor instead.

class Foo
{
ChildObject childObject;

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 7 '05 #2

"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
Dec 7 '05 #3
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"ChrisB" wrote:
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

Dec 7 '05 #4
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" wrote:
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

Dec 7 '05 #5
ChrisB wrote:
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);
...
}
}

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Dec 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Anders Borum | last post: by
6 posts views Thread by Marty | last post: by
7 posts views Thread by relient | last post: by
3 posts views Thread by Jason S | last post: by
5 posts views Thread by Jeremy | last post: by
4 posts views Thread by pbd22 | last post: by
reply views Thread by rosydwin | last post: by

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.