<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d35019691\x26blogName\x3dCinnamon+patrol\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://cinnamonpatrol.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://cinnamonpatrol.blogspot.com/\x26vt\x3d-8713189439990806071', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Cinnamon patrol

Mr and Mrs Lili Wedding

 

In-line and looking hot

I've resisted thus far. No, not resisted - I've just not been interested or had the occasion. But I figure if Laura can post about knitting I can post about my craft. The next question is, will anyone who reads this blog be interested in the slightest? There are some folks from Sandfield who read this blog. Out of that small number (possibly only 1), will there be a momentary arousal?

Now will all that hype, this is bound to be let down.

You will, all of you, be familiar with anonymous methods in C#2.0 and their most immediately useful application as anonymous delegates:

btnFoo.Click += delegate
{
doSomethingGood();
};


They are great and I've been using them a lot where it simplifies the code and makes it easier to read.

Well today I accidentally did something even cooler - mostly by accident.

Imagine a generic function which creates buttons. One of the parameters is an EventHandler that gets hooked up to the button's Click event:

private void MakeButton(string buttonText, EventHandler e)
{
Button b = new Button();
b.Text = buttonText;

if (e != null)
{
b.Click += e;
}
}

Now how do we use our MakeButton function and pass in the EventHandler parameter? Why, with an anonymous delegate of course:

MakeButton("Paris", new EventHandler(delegate{this.txtStatus.Text
= "will be fun";}));

MakeButton("The button", new EventHandler(delegate { this.txtStatus.Text
= "has been clicked"; }));


That's really cool! No little event handling methods lying around. Everything contained with in one line. Easy to read. Easy to write. Okay, I'm done now.

As a treat for anyone who's still reading: Laura has some news... (no, not babies) ... she'll post about it soon!

Labels: ,

 

for this post

 
Anonymous Anonymous Says:

Finally, some decent technical content. Keep this up and I may even become a regular reader ;-)
And congratulations Laura on your news - we are very happy to hear it.

 

Leave a Reply