Re: Assigning "this"

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: ArrayList behavior
    ... Some people suggest that protected member variables violate encapsulation ... >> You have one instance of Book and one reference referring to it. ... declaring a reference of a List implementation is generally ... > database app), and performance is an issue, so I opted to 'protect' the ...
    (comp.lang.java.programmer)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... I can access member variables by name alone. ... returning a reference to a member is bad. ...
    (comp.lang.php)
  • Re: Assigning "this"
    ... Perhaps the OP is searching for the Smalltalk "become" operation, ... I have a large class with a lot of member variables. ... and it's a struct. ... reference altogether. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... I can access member variables by name alone. ... Just to clarify, in my example, I didn't specify returning a reference to a member. ... I think it can be a bit confusing and error prone, especially if you get a long function. ...
    (comp.lang.php)
  • Re: Python advocacy in scientific computation
    ... Intendation as a part of the syntax, ... I find having the compiler enforce indentation rules is a ... have method arguments which get assigned to member variables you don't have ... Can python do "pass by reference"? ...
    (comp.lang.python)