Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

overloading of conversion operators

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Philipp Brune - 05 Sep 2007 09:21 GMT
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<T> type that maintains the values of multiple assignments and
can revert to a previous value. An Example to make it clear :

Revertable<int> ir0 = 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<T> valueList = 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<T> value)
            {
                return value.Current;
            }
        }

Now the problem is, that the implicit conversion operator creates a
new instance of Revertable<T> when an assignment happens. So the
previously assigned value is lost :

Revertable<int> i = 10;
i = 11; // now i is a new instance of Revertable<int> and 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
Jon Skeet [C# MVP] - 05 Sep 2007 09:33 GMT
> 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<T> type 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<T> when 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<int> i = 10;
i += 11;

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

Jon
Alberto Poblacion - 05 Sep 2007 09:49 GMT
> Revertable<int> i = 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<string> s = "Hello";
        s <<= "Bye";

  The <<= would look like you are "pushing" a new value into the stack of
values in s.
Philipp Brune - 05 Sep 2007 10:07 GMT
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<T> provide 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:
>> Revertable<int> i = 10;
>> i += 11;
[quoted text clipped - 9 lines]
>   The <<= would look like you are "pushing" a new value into the stack
> of values in s.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.