I’ve recently been developing both MFC and C# Windows Forms applications and I’ve noticed a difference between the access modifiers on auto-generated event handlers.
In MFC
public:
afx_msg void OnBnClickedOk();
In C# Win Forms
private void button1_Click(object sender, EventArgs e)
So my question is, why are the event handlers declared public in MFC, I would have though the C# way is technically better as it promotes encapsulation (which is perhaps why it was changed).
Thanks
It’s been a while since I did MFC…
Ultimately the way that C# and MFC dispatch messages is different, and equally the two are not just different languages, but entirely different platforms – so it’s not really correct to say that one is ‘better’ than the other, especially simply because of the visibility of an event handling method. There’s nothing stopping you from changing the visibility of a C# handler, in fact in some cases you might need to in order to fake-invoke an event (although there are other more proper architecural ways of achieving the same thing).
Generally a form’s controls and their events are it’s own business by default and that makes perfect sense. Equally, another clue as to C#’s default
privatestance is in the signature of the handlers – they all expect a sender and event data (note that MFC doesn’t) – if the underlying control is private, then there’s no reason to have it public or protected because an external caller cannot provide the correct arguments to the event handler.In that respect, it’s right that the handler be private.