RE: Assigning "this"



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
.