Yesterday I finished translating a C# application to Delphi, this application is part of an SDK.
While translating — even tho' my C# knowledge is limited — I couldn't help not noticing how easy it is to translate C# code to Delphi, well it shouldn't be a surprise since the chief architect behind Delphi and C# is the same person Anders Hejlsberg.
Since this was my first ever C# to Delphi translation, my initial thought was a "one-to-one" translation, however, while analyzing how the application works, I've realized that a "one-to-one" wouldn't be a good idea, normally, but in this case it was the fastest and easiest way in order to replicate original functionality and also be able to go back to the original code and see what was "missed" or "how it was done" down to the each method and variable definition.
Like with almost every programming task, you learn something new — or better ways of accomplishing something —, I've learned how C# handles events and how simple a lot of things can be defined, for example you can have multiple "event listeners or subscribers if you will" — like in Java and others — assigned, and when you call this event, all assigned handlers or subscribers will be called by forwarding the parameters.
Sadly, in Delphi we can't assign multiple event handlers by default, but that doesn't mean we can't simulate it, however it will involve more coding...
NOTE: this application was created using Delphi 2010, should work in any Delphi version that supports generics and even earlier versions if you rewrite specialized versions of TList.
While writing this demo application, I was thinking that it would be a good time to add extra magic that I think it could be useful for novice programmers, so, here's the uMagic.pas unit:
and of course Unit1.pas unit: