Hi all,
i just played around a little with the c# operator overloading features
and an idea came to my mind. You all possibly know the Nullable<T>
Datatype. Now i thought it would be a nice idea to write my own
Revertable<Ttype that maintains the values of multiple assignments and
can revert to a previous value. An Example to make it clear :
Revertable<intir0 = 123;
ir0 = 500;
ir0 = 700;
ir0.Revert(2)
int x = ir0;
now x should be equal to 123
This stuff could be used in some kind of simplified in-memory
transactions to help maintain state consitency.
I started up and wrote something like this :
public class Revertable<T>
{
private List<TvalueList = new List<T>();
private Revertable(T value)
{
valueList.Add(value);
}
private T Current
{
get
{
return valueList[valueList.Count - 1];
}
}
public void Revert(int times)
{
...
}
public static implicit operator Revertable<T>(T value)
{
return new Revertable<T>(value);
}
public static implicit operator T(Revertable<Tvalue)
{
return value.Current;
}
}
Now the problem is, that the implicit conversion operator creates a
new instance of Revertable<Twhen an assignment happens. So the
previously assigned value is lost :
Revertable<inti = 10;
i = 11; // now i is a new instance of Revertable<intand the previously
assigned value is lost
Is there a way to convert to a given instance, instead of creating a new
one ? I saw that it is impossible to overload the assignment operator
in c# . I know that i could easily add an Assign(T value) method to my
revertable class but that would make the code much less readable.
Perhaps i have to take another path and take a language that supports
custom assignment operator if one exists ?!
Thanks in advance for your ideas and suggestions
Philipp