Thursday, September 16, 2004

VJs Tip Of The Day - September 17th 2004

Callback functions

Callback functions are powerful tools and we should know how to use them... In general a callback function is a reference to a method that you pass to another method... When the second method calls the referenced method , it actually calls back to the first method... This might be a confusing terminology but a code snippet from MSDN will probably help make it clearer...

using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp {

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}

Here is the link for you to explore more.... Click Here


PS: I have coined a new term called "Developer's Superstition"... I suffer from this problem often - When code does not seem to behave the way I want it to, knowingly/unknowingly I try things which I know will not work, which I know are technically impossible but still I do just to make myself feel good that I have left no stones unturned... I wonder whether you guys also do such thing... A classic example would be trying to call GC.Collect() method when a StreamReader is not releazing a file even after calling close()...{usually its some other process holding the reference...:-)} I wonder has MS Technologies made me superstitious or is this same everywhere... :-)

No comments: