Re: Assigning "this"

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




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
<Trecius@xxxxxxxxxxxxxxxxxxxxxxxxx> 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

.



Relevant Pages

  • Re: camelCase
    ... When naming private fields the C# guidelines say to use camel casing ... public class SomeObject ... Fortunately, private members are private, so no compiler or client will care ...
    (microsoft.public.dotnet.languages.csharp)
  • Structural question regarding scope of variables
    ... Private Fields ... Private Instance Methods ... different connection strings, based on where the data will need to ...
    (microsoft.public.dotnet.languages.csharp)
  • private field and public property naming conventions
    ... Has anyone found any naming convention guidelines for private fields ... public string MyVariable ... Public Property MyVariable() As String ...
    (microsoft.public.dotnet.framework)
  • Re: Enabling Access to Private Fields
    ... They should still be private to everything ... > access private fields on classes in the assembly that's performing the ... > dynamic compilation. ...
    (microsoft.public.dotnet.security)
  • Re: Changing display geometry (re-post)
    ... > private APPBARDATA abd; ... > private static extern uint SHAppBarMessage(int dwMesssage, ref APPBARDATA ... > private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int ... > public int left; ...
    (microsoft.public.win32.programmer.ui)