Re: Assigning "this"
- From: Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 16 Oct 2007 11:46:45 -0700
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
.
- Follow-Ups:
- Re: Assigning "this"
- From: Fred Mellender
- Re: Assigning "this"
- Prev by Date: Re: <newbie> need a bit help on this code
- Next by Date: Re: Class and Multi-thread safety
- Previous by thread: Re: Assigning "this"
- Next by thread: Re: Assigning "this"
- Index(es):
Relevant Pages
|