By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Assigning "this"

P: n/a
Hello, Newsgroupians:

I have a large class with a lot of member variables. I also have a function
in the class that I would like to change ALL Of the member variables. I am
trying to assign "this" to the result, but I always get the error message,
"Cannot assign to '<this>' because it is read-only."

I've been searching on the Internet, and I notice some C# code is violating
this rule. Perhaps their code is wrong.

For simplicity, suppose I have a Point class that two members: x and y.
Suppose I have two functions called DoubleSizes() and Double().
DoubleSizes() is defined as...

public Point DoubleSizes()
{
return new Point(this.x * 2, this.y * 2);
}

Now, for the Double() function, I'd like to have the following...
public Point Double()
{
this = this.DoubleSizes();
return this;
}

But this doesn't work. Instead, I need to set the result to a temporary
variable and iterate through my variables... EXA:

Point temp = this.DoubleSizes();
this.x = temp.x;
this.y = temp.y;

return this;

Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?" Again,
I've seen some C# code changing the value of this, but are they in violation
of a compile rule?

Thank you, all.
Trecius
Oct 16 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
>Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?"
Only inside a constructor of a struct IIRC. Not for classes.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 16 '07 #2

P: n/a
Mattias Sjögren <ma********************@mvps.orgwrote:
Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?"
Only inside a constructor of a struct IIRC. Not for classes.
You can do it elsewhere in a struct too - but certainly not in classes.

using System;

public class Test
{
struct Foo
{
int y;

public Foo(int x)
{
y = x;
}

public void ChangeTo(int z)
{
this = new Foo(z);
}

public override string ToString()
{
return y.ToString();
}
}

static void Main()
{
Foo f = new Foo(10);
Console.WriteLine(f);
f.ChangeTo(20);
Console.WriteLine(f);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 16 '07 #3

P: n/a
Trecius wrote:
Hello, Newsgroupians:

I have a large class with a lot of member variables. I also have a function
in the class that I would like to change ALL Of the member variables. I am
trying to assign "this" to the result, but I always get the error message,
"Cannot assign to '<this>' because it is read-only."

I've been searching on the Internet, and I notice some C# code is violating
this rule. Perhaps their code is wrong.
Or maybe you're misreading the code. Can you provide links to the code
that you believe is wrong?
For simplicity, suppose I have a Point class that two members: x and y.
An unfortunate choice of example, IMHO. .NET already has a Point type,
and it's a struct. Structs are value types and work in a very different
way from classes. This could be confusing in this context.

That said, let's go with your example nevertheless, with the assumption
that we will ignore .NET's Point struct and assume we have a whole new
Point type that is in fact a class...
Suppose I have two functions called DoubleSizes() and Double().
DoubleSizes() is defined as...

public Point DoubleSizes()
{
return new Point(this.x * 2, this.y * 2);
}

Now, for the Double() function, I'd like to have the following...
public Point Double()
{
this = this.DoubleSizes();
return this;
}
First, note that when assigning something to "this", you aren't copying
values from one instance to another. You would be replacing the "this"
reference altogether. Hopefully you can see why, in a class, it doesn't
make sense to try to replace the instance reference from within the
instance itself.

Beyond that, what is the point of the Point.Double() method? In what
situation do you want to modify every member variable of a class while
returning a new instance of the class? Why would you not simply create
a new instance and use that in place of the old one? You can use
something like MemberwiseClone() to create an exact value-for-value
duplicate of a class, if you're looking for getting two different
instances (for example, you want to modify one without changing the other).
But this doesn't work. Instead, I need to set the result to a temporary
variable and iterate through my variables... EXA:

Point temp = this.DoubleSizes();
this.x = temp.x;
this.y = temp.y;

return this;
Yes, if you want the original instance to be the same instance after the
operation, you will have to modify each and every member variable
individually. But IMHO this is suggestive of a more basic design issue.
If your design is such that you feel you want to replace the class
from within based on an operation that returns a different, new
instance, I would suggest there's a problem with the design itself.
Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?" Again,
I've seen some C# code changing the value of this, but are they in violation
of a compile rule?
Again, if you could post links to the examples of code you say does
this, that would be helpful. I think it's likely the code isn't doing
what you think it's doing.

In addition, it would be helpful if you could try to clarify what it is
exactly you're trying to do. The code you're posting doesn't fit
exactly with the words you're writing. In particular, you seem to just
want to copy values from one instance to another, but the code you've
posted isn't doing that (even if the compiler let you). It's replacing
the instance reference altogether.

Do you want to replace the reference? If so, how would you expect that
to work? If not, then why are you trying to write code that would (if
it compiled) replace the reference? Are you confused about the
difference between a struct and a class?

Pete
Oct 16 '07 #4

P: n/a
Perhaps the OP is searching for the Smalltalk "become" operation, wherein
A.become(B) would swap all references to A with a reference to B, and
vice-versa. There is no such function in C# (or in the CLR, so far as I
know), although it would not be too hard to implement. See
http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/104551.

The semantics might be problematic in C# unless A and B were of the same
class. Owing to Smalltalk's runtime type checking this is not a problem
(but of limited utility, admittedly, when the objects are of different
classes).

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Trecius wrote:
>Hello, Newsgroupians:

I have a large class with a lot of member variables. I also have a
function in the class that I would like to change ALL Of the member
variables. I am trying to assign "this" to the result, but I always get
the error message, "Cannot assign to '<this>' because it is read-only."

I've been searching on the Internet, and I notice some C# code is
violating this rule. Perhaps their code is wrong.

Or maybe you're misreading the code. Can you provide links to the code
that you believe is wrong?
>For simplicity, suppose I have a Point class that two members: x and y.

An unfortunate choice of example, IMHO. .NET already has a Point type,
and it's a struct. Structs are value types and work in a very different
way from classes. This could be confusing in this context.

That said, let's go with your example nevertheless, with the assumption
that we will ignore .NET's Point struct and assume we have a whole new
Point type that is in fact a class...
>Suppose I have two functions called DoubleSizes() and Double().
DoubleSizes() is defined as...

public Point DoubleSizes()
{
return new Point(this.x * 2, this.y * 2);
}

Now, for the Double() function, I'd like to have the following...
public Point Double()
{
this = this.DoubleSizes();
return this;
}

First, note that when assigning something to "this", you aren't copying
values from one instance to another. You would be replacing the "this"
reference altogether. Hopefully you can see why, in a class, it doesn't
make sense to try to replace the instance reference from within the
instance itself.

Beyond that, what is the point of the Point.Double() method? In what
situation do you want to modify every member variable of a class while
returning a new instance of the class? Why would you not simply create a
new instance and use that in place of the old one? You can use something
like MemberwiseClone() to create an exact value-for-value duplicate of a
class, if you're looking for getting two different instances (for example,
you want to modify one without changing the other).
>But this doesn't work. Instead, I need to set the result to a temporary
variable and iterate through my variables... EXA:

Point temp = this.DoubleSizes();
this.x = temp.x;
this.y = temp.y;

return this;

Yes, if you want the original instance to be the same instance after the
operation, you will have to modify each and every member variable
individually. But IMHO this is suggestive of a more basic design issue.
If your design is such that you feel you want to replace the class from
within based on an operation that returns a different, new instance, I
would suggest there's a problem with the design itself.
>Again, this is a small example. In my case, I have about twenty
variables that I'd like to reassign. Is it just possible to reassign
"this?" Again, I've seen some C# code changing the value of this, but
are they in violation of a compile rule?

Again, if you could post links to the examples of code you say does this,
that would be helpful. I think it's likely the code isn't doing what you
think it's doing.

In addition, it would be helpful if you could try to clarify what it is
exactly you're trying to do. The code you're posting doesn't fit exactly
with the words you're writing. In particular, you seem to just want to
copy values from one instance to another, but the code you've posted isn't
doing that (even if the compiler let you). It's replacing the instance
reference altogether.

Do you want to replace the reference? If so, how would you expect that to
work? If not, then why are you trying to write code that would (if it
compiled) replace the reference? Are you confused about the difference
between a struct and a class?

Pete

Oct 16 '07 #5

P: n/a
Fred Mellender wrote:
Perhaps the OP is searching for the Smalltalk "become" operation, wherein
A.become(B) would swap all references to A with a reference to B, and
vice-versa.
Vice-a-versa? As in all references to B also are replaced with
references to A?

Anyway, perhaps that's what the OP wants, but if so they (one hopes)
have a higher-level design issue that they are trying to address with
that behavior.

Since C# doesn't allow assignment to the "this" of a class, they aren't
going to get the specific behavior. So the only remaining question is
really what probably are they really trying to solve? Then answers can
be provided more along the lines of what C# actually does provide.

Pete
Oct 16 '07 #6

P: n/a
Thank you, Mr. Sjogren and Mr. Skeet, for a quick response.

Also, thank you to Mr. Mellender and Mr. Duniho for alternative approach to
my predicament.

Thank you, all, again.
Trecius

"Trecius" wrote:
Hello, Newsgroupians:

I have a large class with a lot of member variables. I also have a function
in the class that I would like to change ALL Of the member variables. I am
trying to assign "this" to the result, but I always get the error message,
"Cannot assign to '<this>' because it is read-only."

I've been searching on the Internet, and I notice some C# code is violating
this rule. Perhaps their code is wrong.

For simplicity, suppose I have a Point class that two members: x and y.
Suppose I have two functions called DoubleSizes() and Double().
DoubleSizes() is defined as...

public Point DoubleSizes()
{
return new Point(this.x * 2, this.y * 2);
}

Now, for the Double() function, I'd like to have the following...
public Point Double()
{
this = this.DoubleSizes();
return this;
}

But this doesn't work. Instead, I need to set the result to a temporary
variable and iterate through my variables... EXA:

Point temp = this.DoubleSizes();
this.x = temp.x;
this.y = temp.y;

return this;

Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?" Again,
I've seen some C# code changing the value of this, but are they in violation
of a compile rule?

Thank you, all.
Trecius
Oct 16 '07 #7

P: n/a

Define a private inner-class to hold all of your private fields (I
hope you don't have any public fields!) and then in your constructor
create an instance of this private class and then use that for all
private field access.

Then in the situation where you need to assign everything, you can do
it by assigning the private class.

public class Point {
private class PointData {
public int x;
public int y;
}

private PointData data = new PointData();

public int X {
get { return data.x; }
set { data.x = value; }
}

public int Y {
get { return data.y; }
set { data.y = value; }
}

public void Become(Point other) {
data = other.data;
}
}

Note that this is a reference assignment so it can have unintended
side-effects (depends on what your intended behavior, you can do a
clone instead of assignment).

It's not exactly the same implementation but it's similar to Memento
design pattern. http://www.dofactory.com/Patterns/PatternMemento.aspx

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 16 Oct 2007 10:57:00 -0700, Trecius
<Tr*****@discussions.microsoft.comwrote:
>Hello, Newsgroupians:

I have a large class with a lot of member variables. I also have a function
in the class that I would like to change ALL Of the member variables. I am
trying to assign "this" to the result, but I always get the error message,
"Cannot assign to '<this>' because it is read-only."

I've been searching on the Internet, and I notice some C# code is violating
this rule. Perhaps their code is wrong.

For simplicity, suppose I have a Point class that two members: x and y.
Suppose I have two functions called DoubleSizes() and Double().
DoubleSizes() is defined as...

public Point DoubleSizes()
{
return new Point(this.x * 2, this.y * 2);
}

Now, for the Double() function, I'd like to have the following...
public Point Double()
{
this = this.DoubleSizes();
return this;
}

But this doesn't work. Instead, I need to set the result to a temporary
variable and iterate through my variables... EXA:

Point temp = this.DoubleSizes();
this.x = temp.x;
this.y = temp.y;

return this;

Again, this is a small example. In my case, I have about twenty variables
that I'd like to reassign. Is it just possible to reassign "this?" Again,
I've seen some C# code changing the value of this, but are they in violation
of a compile rule?

Thank you, all.
Trecius
Oct 16 '07 #8

P: n/a
Samuel R. Neff wrote:
[...]
Note that this is a reference assignment so it can have unintended
side-effects (depends on what your intended behavior, you can do a
clone instead of assignment).
If those side-effects are not desired, an alternative to using a clone
of the class would be to simply define a struct to contain the data
instead, so that each instance of the class has its own copy of the data
implicitly.

Pete
Oct 16 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.