Friday, November 30, 2007 - 07:59

Events & Inheritance

I came across something odd the other day concerning events and inheritance.

I have a class which inherits from a TextEdit. I created this derived class so that I could add some behaviour to the Click event, without having to hook up the event for every TextEdit I use - so that I could just myTextEdit instead. My first approach was to hook up a OnClick event handler to the Click event in the constructor:


public myTextEdit()
{
InitializeComponent();
Click += new EventHandler(myTextEdit_Click);
}

void myTextEdit_Click(object sender, EventArgs e)
{
// do stuff
}


But while the code hooking up the event was executed, my event handler was never run. So my second approach was to instead override the eventhandler:


protected override void OnClick(EventArgs e)
{
base.OnClick(e);

// do stuff
}

and this worked perfectly.

But I don't understand why the first approach didn't work. While the two approaches are doing radically different things under the covers, they should amount to much the same thing in the end: the event handler should be fired (since it's the base class that's actually raising the event, not the derived class). Maybe I'm just missing something obvious?

I did a bit of reading, and it seems that the overriding is the 'better' way to go, since if you're creating a derived class you're the publisher of the event, and shouldn't be a subscriber. This totally makes sense, but even they seem to think that either method should work. After all, if I’d overridden the base class without touching the events and event handlers, then instantiated my derived class, and then created a 3rd class and hooked up event handlers in that class to the event, I’d expect them to be fired. How does the internal event firing mechanism differ if the event handlers are hooked up in the derived class rather than a 3rd external class?

The next step, of course, was to write a set of test classes to try this out in a simple demo - and just to confuse the issue, both methods worked! So either my demo wasn't set up correctly, or it only occurs on GUI related .NET methods like OnClick(). I'll keep investigating, and report back if I find anything useful... but in the meantime, if you have any ideas, please leave a comment!

Edit: Okay, I think I've figured out what's happening, thanks to a bit of experimenting and this thread. If you declare an event as virtual, the base class and the derived class each have their own delegate list, which is where the problem comes in. If you have an external class which subscribes to the derived class's event, but doesn't subscribe to the base class's event, then trigger the event in the base class, the event isn't fired in the base class (since the delegate is null), and therefore it derived class's event handler can't react to it. I'm not sure if that is what's happening in this case, since the actual event and the base handlers are declared somewhere within the depths of .NET and DevExpress, but it would make sense - and would explain why my simple demo app works perfectly.

Labels:

Tuesday, November 27, 2007 - 07:40

Toyrun 2007

The 2007 Toyrun was last Sunday, and it was a particularly special one as it was the 25th Anniversary.

As usual, it was great fun. They estimate there were 6000 bikes at Maynardville, although it seemed like more! And this time I was riding in the thick of it - bikes as far as you could see in front of you, bikes next to you, and if you got a chance to turn around, bikes as far as you could see behind you! It really is the most amazing feeling, and the atmosphere is incredible. So many people lining the roads, clapping and cheering you on, trying to touch the bikers - it really makes you feel good. And of course, the most important part is the toys for the kids; they had two full trailers of toys at Maynardville alone.

We had tried to organise all the bikers from work to go together. Unfortunately it ended up with only two of us being able to make it, and then we couldn't find each other in the crowd at Ottery - and looking at the pic below, you can see why - and that was only a tiny section of the parking area! (Mine's the one on the right, in the front of the photo).
IMAGE_00116
Unfortunately, it's crappy cellphone photos again, but here are a couple of photos from Maynardville:
IMAGE_00119 IMAGE_00118

Labels:

Thursday, November 08, 2007 - 06:36

New Google Things

As you can probably tell from that scintillating title, I've been working really hard lately and am completely exhausted. Hence the lack of posts in the past few weeks (or months).

But there's a bunch of stuff I want to talk about - like flow, and how it's easy to work hard when you've got flow; and my new TomTom One that I bought; and the Wii that I bought. But right now I'm struggling to form complete sentences, so I'll stick to an easy topic.

There are two new developments from Google that affect me. There's a whole lot of new stuff from them, of course, like updated docs and the new google phone OS, but that's stuff that I don't use so isn't really relevant to me right now.

What is relevant is the GMail upgrade. At first I didn't notice it, to be honest; when I did, it was mostly negative. A lot of people have said that it's amazingly fast now, but I really find it to be a lot slower than before. It's slower to load, slower to bring up your emails, slower to send. And often after I apply a label or achive a conversation, and then try to close my gmail tab, I get the message about how the server is still busy and I'll lose my changes if I navigate away from the page. Leave it a few seconds, and you still get the message; leave it a few minutes, and you can navigate away all you want. So for me, the Gmail upgrade is largely negative.

What isn't really new from Google, but is new to me, is Google Bookmarks. Often I'll come across something at work which looks cool, and I want to remember when I get home. So I end up sending myself email with the link. This works, but it's not really elegant. So today I went looking and discovered Google Bookmarks. It's really simple, and I know that there are other places that do similar but more advanced stuff, but this does everything I want. I don't want to share bookmarks, or turn it into a Web 2.0 social networking thing - I just want a list of things that I can add to at work, and read and remove at home. It's a bit schleppy to use manually, since you have to add the url, the title, etc manually - it doesn't pick it up from the page itself. But this does allow you to bookmark pages other than the current page. If you don't want to do it manually, but you don't want to install the google toolbar, a great firefox extension is GBookmarks. The only downside of it is that you can only bookmark the current page; the good side is that it picks up the title and all that automatically. It also allows you to create folders and organise your bookmarks, which you can't do on the Google Bookmarks site.

That's it for today - more posts on that other stuff coming soon.

Labels: