By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

overloading of conversion operators

P: n/a
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
Sep 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sep 5, 9:21 am, Philipp Brune <her...@gmx.dewrote:
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.
<snip>
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 :
Indeed - that's how the assignment operator works, everywhere. The
previous value is lost. Having a type where assignment uses a previous
value feels very counterintuitive to me.

Personally I don't like overloading operators in any but the most
clearcut cases (TimeSpan, DateTime etc) but you might consider
overloading + instead. Then you could do:

Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...

Jon

Sep 5 '07 #2

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...
Maybe the OP could use some other operator which is less common than +.
For example:
Revertable<strings = "Hello";
s <<= "Bye";

The <<= would look like you are "pushing" a new value into the stack of
values in s.
Sep 5 '07 #3

P: n/a
Jon, Alberto

thank you both for the quick reply :-) I had the idea to "abuse" a
binary operator too, something like

r ^= 12345

and additionaly let Revertable<Tprovide a Xor Method if T itself has
an ^ operator defined.

The other way would be to provide a property setter to take

r.V = 12345

but both of them are easy to forget and less comfortable. So i have
to take that my idea (in that special way i wanted it) is impossible to do.

Philipp

Alberto Poblacion schrieb:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...

Maybe the OP could use some other operator which is less common than
+. For example:
Revertable<strings = "Hello";
s <<= "Bye";

The <<= would look like you are "pushing" a new value into the stack
of values in s.

Sep 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.