How to assign a reference to a class member to another class member?



Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes:

I'd like to assign a reference to an arbitrary class member to another class member, so that I can later mutate the stored reference. I'd like to use this for an undo function - basically, when you make a change to a class member value, you call SaveMemberValue() to store the reference and original value. Later, if necessary, you can Restore() the original value without knowing at compile time which member was changed.

The illegal code below illustrates what I'm trying to accomplish. Another approach that would work in my application is to have separate Restore() functions for each class member, but I think that's ugly.

I'd appreciate any help with this. Thanks in advance.



using System;


class RestoreTest
{
private int a;
private int b;
private int c;
private int savedValue;
//ReferenceToInt is a bogus type
private ReferenceToInt savedValueReference;

public static void Main(string[] args)
{
RestoreTest r = new RestoreTest();
r.a=1;
r.b=2;
r.c=3;
Console.WriteLine("r's values are {0}, {1}, and {2}",
r.a,r.b,r.c);
//Output: r's values are 1, 2, and 3
r.Mutate(ref r.a,6);
Console.WriteLine("r's values are {0}, {1}, and {2}",
r.a,r.b,r.c);
//Output: r's values are 6, 2, and 3
r.Restore();
Console.WriteLine("r's values are {0}, {1}, and {2}",
r.a,r.b,r.c);
//Output: r's values are 1, 2, and 3
}
private void Mutate(ref int member, int val)
{
member=val;
SaveMemberValue((referenceToInt)member,val);
}
private void SaveMemberValue(ReferenceToInt referenceToMember,
int val)
{
savedValueReference=referenceToMember;
savedValue=val;
}
private void Restore()
{
savedValueReference=savedValue;
}
}
.



Relevant Pages

  • Re: How to assign a reference to a class member to another class member?
    ... I'd like to assign a reference to an arbitrary class member to another ... Later, if necessary, you can Restore() the original value ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating tasks on restart: userspace vs kernel
    ... Clone with pid: ... To restart processes from userspace, there needs to be a way to ... The use case is to restore as much as possible to the same state as ... nsproxy image have reference to uts_ns image. ...
    (Linux-Kernel)
  • Re: Referencing [,] element as [ ]?
    ... Why not just create your own collection class and implement the indexers to do what you want? ... I know the Array.Length property will give me the length of the array as 42, but I'd like to be able to reference specific elements. ... private int m_rows; ... public CellRef this{ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sysinfo.dl,l Sysinfo2.dll and Rundll Error Message
    ... How can I restore a clean one back on it? ... You av software deleted the malware, but didn't remove the references to the files in the Registry, so they're still called when Explorer.exe is called. ... If you find a reference to the file not attached to Explorer.exe or other system files, you can probably just delete the entry, press F3 to continue the search. ...
    (microsoft.public.windowsxp.general)
  • Re: How to use COM in php?
    ... > Declaring a char array as a class member variable allocates storage ... Whether it's a class member or not has ... You could not return a pointer or reference to an automatic std::string, ... container classes make C++ much more like PHP than if you use C-style ...
    (comp.lang.php)