this keyword problem

T

Timothy V

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.
 
J

Jon Skeet [C# MVP]

Timothy V said:
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?
 
M

Morten Wennevik

Hi Timothy,

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

Timothy V

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.
 
F

Flare

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
 
G

Gawel

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)
 
U

Uri Dor

'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.
 
D

Daniel O'Connell [C# MVP]

Flare said:
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)
 
T

Ted Miller

Daniel O'Connell said:
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.
 
D

Daniel O'Connell [C# MVP]

Ted Miller said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top