473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2452
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Anders Borum | last post by:
Hello! I'm wondering what the recommendations are for using the "this" keyword? The problem is that I really like to use the "this" keyword, but it tends to make the code more verbose (which...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
1
by: goe | last post by:
Hi, Im my function I need to use focus() and select() based on the "this" keyword. example: onchange="validate(this)" function validate(input) {
3
by: Jason S | last post by:
I have a rough understanding of what the "this" keyword refers to in Javascript but am not very clear on it. Is there a good reference that explains it well?
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
4
by: pbd22 | last post by:
Hi. I am wondering what do I replace the "this" statement with to get the appropriate container/response. When I try: cal.prototype.dates(cal.days.day); I get
5
by: Polaris431 | last post by:
I notice that the Dataset designer generates code for properties that look like this: public string ID { get { try { return ((string) (this)); } catch (System.InvalidCastException e) {
11
by: moltendorf | last post by:
Alright, another question to pin on the experts. :D I have an issue with "this" keyword in this case. The way I got around it before was to use a window property, but in this case I'm trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.