473,574 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(thi s);

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 2465
>Any way around this?

Initialize it in the constructor instead.

class Foo
{
ChildObject childObject;

Foo()
{
childObject = new ChildObject(thi s);
}
}
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(thi s);

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(thi s);
}

// ...

}
or use another reference to the instance of the containing object, e.g.

class Mother
{
public static void StaticMethod(Mo ther 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(thi s);

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(thi s);

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(thi s);

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(thi s);

public SomeClass()
{
...
}
}

instead, do this:

public class SomeClass
{
ChildObject childObject;

public SomeClass()
{
childObject = new ChildObject(thi s);
...
}
}

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.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
1576
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 some don't like). Your oppinion please? ... internal void PageDeleted(Page page) {
6
2702
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 inherit from a QuickFix.Application class. I have this object: private static SocketInitiator qfxInitiator; When instantiated, its constructor...
7
2220
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the...
1
2170
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
1622
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
2225
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"); this.textbox.type = "text"; this.textbox.addEventListener("blur", this.doThings, false);
4
1464
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
1492
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
1546
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 keep everything within the context of the object. So... Basically, this is referring to what I wanted it to refer to before (well, it worked in...
0
7807
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8241
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8100
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6456
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5624
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.