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. 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
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]
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
'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.
> 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
> 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
"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
"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.
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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>...
|
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...
|
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...
| |
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...
|
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");...
|
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...
|
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();...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |