I know the answer (it's 42)

A blog on coding, .NET, .NET Compact Framework and life in general....

October, 2006

Posts
  • I know the answer (it's 42)

    using anonymous methods for adapter code

    • 0 Comments

    Sometime back I had blogged about whether programmers really use anonymous methods. Recently I found a problem for which anonymous method felt like a natural choice.

    I am currently coding an UI glue layer. I need it because I have some stable back-end code (with event-handlers) which I have to tie with some new UI. The problem is that the event handler signatures of the backend matches some other UI technology that was used before (which I cannot modify). The signature doesn't match the one required by the UI technology I'm glueing it to. Interestingly these handlers were not added using += but using some methods which got the handler as a parameter.

    The first solution that comes to mind is to change the method that adds the handler so that it creates an instance of some adapter class. The adapter class has [in] and [out] event handlers. The [in] handler matches the new UI-technology specific signature and the [out] matches the older signature. Objects of this class is created by the API and the [in] handler is tied to the UI-events and when it gets fired it simply calls the [out] handler.

    The problem with this approach is defining all the adapter classes and coding them up. Anonymous methods with outter variable reference solves this problem elegantly.

    public override object AddFocusChangeEventHandler(MyEventHandler eventHandler)

    {

    UISpecificEventHandler uiHandler =

    delegate(object sender, UISpecficEventArgs e)

    {

    // Convert sender and UISpecificEventArgs to what

    // matches MyEventHandler and call it.

    eventHandler(converterSender, convertedArgs);

    };

    UI.AddFocusChangedEventHandler(uiHandler );

    return uiHandler;

    }

    With this I'm easily done as C# automatically generates hidden classes to wrap the anonymous method when there is a reference to an outter variable (variable defined outside the anonymous method, eventHandler in this case).

  • I know the answer (it's 42)

    What's your phone number

    • 2 Comments

    Yesterday on our post-lunch walk (we take a brisk walk around the campus after lunch when its not too hot) we were discussing phone numbers. Cell phone numbers in India are 10 digits long. Remembering numbers have become difficult, specially so for your own number because you never dial it yourself!!!

    So I asked everyone around to guess how I remember it....

    I don't, I just store it as this on the phone. What else do you expect from a C# programmer???

  • I know the answer (it's 42)

    One of the most peculiar machine which I've ever seen

    • 4 Comments

    I went to a place of worship in Hyderabad where I saw this peculiar machine. I don't know what to call it!! Its a electrical machine that plays the drum and bells (??). I just couldn't figure out why they couldn't use a music player instead. Unfortunately (or was it fortunately??) it was powered off at that moment.

     

     

  • I know the answer (it's 42)

    We just finished celebrating our biggest festival

    • 0 Comments

    Durga puja is the biggest festival for Bengali's all over the world. This is the time when we worship the Goddess Durga, the Goddess  of strength, the symbol of the victory of good over evil. This time around my home town of Kolkata gets into festival mode. Most offices, business, schools, shuts down for over 5 days and all work stops. Thousands of people flock the street of the city in their finest clothes, visiting places of worship, eating all sorts of local fast food and generally have a good time.

    Unfortunately I couldn't visit home this year. But even in Hyderabad (where Microsoft India is situated) there are huge celebrations. The best part is that people from all over the world celebrate Durga Puja with us (Like this girl taking part in the Dhunuchi Dance)

Page 1 of 1 (4 items)