Wednesday, October 1, 2014

Not doing explicit interface implementation waste our time on debugging

If you received this kind of error, you'll wonder why MSDN writers can't be bothered to implement interfaces explicitly

Additional information: The target '__Page' for the callback could not be found or did not implement ICallbackEventHandler.






I thought I followed the sample code from MSDN to a tee: http://msdn.microsoft.com/en-us/library/vstudio/ms178210(v=vs.100).aspx


Had MSDN writers written this code..

public void RaiseCallbackEvent(String eventArgument)
{
 if (catalog[eventArgument] == null)
 {
  returnValue = "-1";
 }
 else
 {
  returnValue = catalog[eventArgument].ToString();
 }
}

public String GetCallbackResult()
{
 return returnValue;
}

..using explicit interface implementation..
void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
{
 if (catalog[eventArgument] == null)
 {
  returnValue = "-1";
 }
 else
 {
  returnValue = catalog[eventArgument].ToString();
 }
}

ICallbackEventHandler String GetCallbackResult()
{
 return returnValue;
}


..there would be no time wasted on debugging and googling for solution, especially when we are fond of Ctrl+F5 instead of F5. We often need to use Ctrl+F5 it's faster, F5 slows down the coding-running cycle


Explicitly implementing those interface methods, it would be readily apparent that we just forgot to add the interface to class as the explicit interface implementation would not compile if we forgot to add the interface:

public partial class Default : System.Web.UI.Page, ICallbackEventHandler



Happy Coding!

No comments:

Post a Comment