S
Steve Goldman
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;
}
}
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;
}
}