Monday, August 22, 2011

Small things learned today: Raise Base Class Event in Derived Classes.

I have to admit that I did not know this before today.

If I have a base class that have controls, I can not directly subscribe to the events invoked by the controls of this class (or, more generally, I can not directly subscribe to any events declared by the base class, but in my case I was interested in button click events).

I have to use a simple technique to achieve my goal: In the base class, provide and EventHandler (my button is called "Run Draw", hence the names)

public event EventHandler RunDrawClicked;
protected virtual void OnRunDrawClicked(EventArgs e)
{
EventHandler handler = RunDrawClicked;
if (handler != null)
{
handler(this, e);
}
}

base class can subscribe to its own button click, of course

protected void btnRunDraw_Click(object sender, System.EventArgs e)
{
MessageBox.Show("base");
OnRunDrawClicked(e);
}

and the derived class can subscribe to the event provided by the base class

protected override void OnRunDrawClicked(EventArgs e)
{
MessageBox.Show("derived");
base.OnRunDrawClicked(e);
}

Reference:

How to: Raise Base Class Events in Derived Classes by . Also posted on my website

Monday, August 15, 2011

Third Party DLL random thoughts ...

I'm always very happy then a third party dll or SDK comes with a code sample, in any language. I think this is the best what developers can do to ensure that users of their libraries will not have problems. It helped me a number of times (Oh, so I have to pass THIS to the function ... why didn't they mention it in the manual?).

But I like it a bit less when the code comes as a neat Visual Studio solution which no one obviously tested to make sure it compiles.

So I made an empty header file bc_Content_Decoder_Constants.h and added it to the project and commented out the body of a function that used the constants from the header.

Now I'm up to the next challenge: I can run the application in release mode, but not in debug mode.

Looks a bit vague.

I spent a while playing with linker settings and application manifest, but eventually ended up debugging the application in my head. In this particular case it was not as hard as it may sound - just looked at what functions were called in what sequence and duplicated that in my C# "throwaway" app and found a workaround to my problem. Sill, sample code from the third party was better than nothing.

by . Also posted on my website