473,472 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

this keyword problem

Hi everyone,
I'm having trouble with the keyword this inside a class. The following code
is an example of the problem, not what I am actually doing.

public class C
{
public C() {}
public string s;
public void Function()
{
C c = new C();
this = c;
}
}

Basically it brings a compiler error CS1604 (Cannot assign to 'variable'
because it is read-only).

My question is, how can I do this? Is it possible?

Thank you,

Tim.
Nov 16 '05 #1
10 1504
Timothy V <tr****@msn.com> wrote:
I'm having trouble with the keyword this inside a class. The following code
is an example of the problem, not what I am actually doing.

public class C
{
public C() {}
public string s;
public void Function()
{
C c = new C();
this = c;
}
}

Basically it brings a compiler error CS1604 (Cannot assign to 'variable'
because it is read-only).

My question is, how can I do this? Is it possible?


You can't. Why would you want to?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Timothy,

Eh, what are you trying to do? You can't change 'this', and why would you want to?

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
hehehe, well, I have a method that deserializes a an xml file and i would
like to assign 'this' as the object that the deserializer returns.

Its ok, i was just wondering if it was at all possible. I'll do it another
way.

Thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Timothy V <tr****@msn.com> wrote:
I'm having trouble with the keyword this inside a class. The following
code
is an example of the problem, not what I am actually doing.

public class C
{
public C() {}
public string s;
public void Function()
{
C c = new C();
this = c;
}
}

Basically it brings a compiler error CS1604 (Cannot assign to 'variable'
because it is read-only).

My question is, how can I do this? Is it possible?


You can't. Why would you want to?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
'this' is a pointer to the "current" object, which is the object on
which the method was called.
if you reached Function(), which is a non-static method, you definitely
already have an instance of class C.
No, it is not possible to assign to 'this', and if you think hard,
there's no point in doing that in the first place.

Timothy V wrote:
Hi everyone,
I'm having trouble with the keyword this inside a class. The following code
is an example of the problem, not what I am actually doing.

public class C
{
public C() {}
public string s;
public void Function()
{
C c = new C();
this = c;
}
}

Basically it brings a compiler error CS1604 (Cannot assign to 'variable'
because it is read-only).

My question is, how can I do this? Is it possible?

Thank you,

Tim.

Nov 16 '05 #5
> No, it is not possible to assign to 'this', and if you think hard,
there's no point in doing that in the first place.


There would be a point. Conceptual you could assign the current objects this
pointer to another object's this pointer. This would make them equal. I
would be just like making a reference copy, just the other way around.

But then again...you can´t, and you could obtain the above logic in a more
transparent way.

Anders Jacobsen
Nov 16 '05 #6
> hehehe, well, I have a method that deserializes a an xml file and i would
like to assign 'this' as the object that the deserializer returns.

Its ok, i was just wondering if it was at all possible. I'll do it another
way.


Try to do it in static method of your class. This pattern is used in may
..net classes,
for example Image Image.FromFile(String path)

--
Gawel
-------------------------------
Pierwszy ³yk z pucharu nauk przyrodniczych czyni ateist±, ale na dnie
pucharu czeka Bóg.
Werner Heisenberg
Nov 16 '05 #7

"Flare" <no****@sorry.dk> wrote in message
news:On**************@tk2msftngp13.phx.gbl...
No, it is not possible to assign to 'this', and if you think hard,
there's no point in doing that in the first place.
There would be a point. Conceptual you could assign the current objects
this
pointer to another object's this pointer. This would make them equal. I
would be just like making a reference copy, just the other way around.


That actually doesn't work. this is an argument(argument 0), assigning it
would result in changing the this pointer *only* for the current method. The
only way to change that would be to change all this pointers into ref
arguments, which is far less enticing, IMHO.

As such assigning the this pointer, as it is, has no point. Thats a
considerable reason why the language offers *no* way to make such
assignments(of course, the general danger of the assignments is almost
certainly why the *runtime* chose not to make it possible by default)

But then again...you can´t, and you could obtain the above logic in a more
transparent way.

Anders Jacobsen

Nov 16 '05 #8

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
That actually doesn't work. this is an argument(argument 0), assigning it
would result in changing the this pointer *only* for the current method. The only way to change that would be to change all this pointers into ref
arguments, which is far less enticing, IMHO.

As such assigning the this pointer, as it is, has no point. Thats a
considerable reason why the language offers *no* way to make such
assignments(of course, the general danger of the assignments is almost
certainly why the *runtime* chose not to make it possible by default)


I think he's trying to get behavior as if operator = was overloaded. So the
goal of "assigning" to this would not be to mess with the this pointer
itself, but rather to get some sort of duplication of the fields from the
rhs instance to the current object.

Since operator = is not overloadable in C#, this is a non-starter regardless
of how one feels about the idea.
Nov 16 '05 #9
Yup, that was all i really wanted to do.

I ended up using static methods instead.
"Ted Miller" <te*@nwlink.com> wrote in message
news:10*************@corp.supernews.com...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
That actually doesn't work. this is an argument(argument 0), assigning it
would result in changing the this pointer *only* for the current method.

The
only way to change that would be to change all this pointers into ref
arguments, which is far less enticing, IMHO.

As such assigning the this pointer, as it is, has no point. Thats a
considerable reason why the language offers *no* way to make such
assignments(of course, the general danger of the assignments is almost
certainly why the *runtime* chose not to make it possible by default)


I think he's trying to get behavior as if operator = was overloaded. So
the
goal of "assigning" to this would not be to mess with the this pointer
itself, but rather to get some sort of duplication of the fields from the
rhs instance to the current object.

Since operator = is not overloadable in C#, this is a non-starter
regardless
of how one feels about the idea.

Nov 16 '05 #10

"Ted Miller" <te*@nwlink.com> wrote in message
news:10*************@corp.supernews.com...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
That actually doesn't work. this is an argument(argument 0), assigning it
would result in changing the this pointer *only* for the current method.

The
only way to change that would be to change all this pointers into ref
arguments, which is far less enticing, IMHO.

As such assigning the this pointer, as it is, has no point. Thats a
considerable reason why the language offers *no* way to make such
assignments(of course, the general danger of the assignments is almost
certainly why the *runtime* chose not to make it possible by default)


I think he's trying to get behavior as if operator = was overloaded. So
the
goal of "assigning" to this would not be to mess with the this pointer
itself, but rather to get some sort of duplication of the fields from the
rhs instance to the current object.

Since operator = is not overloadable in C#, this is a non-starter
regardless
of how one feels about the idea.


Perhaps, but if you are already able to access this, you can access the
fields, ;).

I can see the point, however I disagree with it as it entirely breaks C#
semantics and really just isn't a good idea. I also am not particularly
convinced you could write it with an overloadable = operator without runtime
support.
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jack | last post by:
Hi, Is it recommended to use the var keyword when declaring a variable? IE lets me create a variable with var, then create the same one again with no problem. Also it lets me create a...
3
by: Arthur Dent | last post by:
Hi all, Im just curious, what is the purpose of the keyword "Overloads" in VB nowadays? I understand conceptually what overloads are and what they do, but im a little puzzled, because if you...
18
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream>...
8
by: blaw1010 | last post by:
I currently am looking for a solution to solve a 'keyword search' problem. I am using the 'like' functionality to retrieve the values that match the keyword that is entered. However, how can I make...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
6
by: tom | last post by:
Hi I try to check whether a given input is keyword or not. However this script won't identify keyword input as a keyword. How should I modify it to make it work? #!usr/bin/env python import...
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: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
10
by: Stevo | last post by:
Simplified example code which can be copy/pasted and will exhibit the problem. var ob={id:2}; ob.alertid=function(){alert("id="+this.id);} ob.funcs=; ob.testalert=function(){ this.alertid();...
1
by: prigupta2 | last post by:
hello friends i am in problem my client does not want to search his side xxxxx keyword when any one search his site xxxxxxxx keyword its come on google front page is there any way to...
0
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,...
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
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...
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.