Tuesday, April 22, 2008

Using Delegates (i.e. Function Pointers)

Ever since learning how to use function pointers in C, I've always been a fan of using them to help make code a bit more usable, especially when you've got a state machine. Today, as I'm working on a Wizard UI for a desktop application I came across a typical scenario for using a function pointer. Depending on the stage of the Wizard you're in, a button will have to do separate things.

That got me to thinking that most managed developers simply don't understand the power and utility of delegates, but instead simply consider them a necessity when using Control.Invoke or creating custom events. Sure, in my case I could have a switch statement in the click handler and do logic there, or I could unhook the click handler from one method and hook it to another, but those all seem ugly and a pain in the ass to me. A simple function pointer change is all you need. So I decided I'd throw together a really simple example of how you would use a delegate to change the behavior of a Button click.

Let's assume that we have a button that we want to click, and when it's clicked it will do one of 4 things, depending on the state of our application. We'll just use a messagebox here to give you the idea - what it does is up to you- it's a function after all.

public
void FunctionA()
{
MessageBox.Show(
"FunctionA");
}

public
void FunctionB()
{
MessageBox.Show(
"FunctionB");
}

public
void FunctionC()
{
MessageBox.Show(
"FunctionC");
}

public
void FunctionD()
{
MessageBox.Show(
"FunctionD");
}

To simulate the different "states" I simply added a ListBox (called functionList) to the Form and manually added the function names to it in the Form's constructor. Sure, I could have used Reflection to be clever and populate the list, but I'm tryiong to keep it simple and show delegates.

functionList.Items.Add("FunctionA");
functionList.Items.Add(
"FunctionB");
functionList.Items.Add(
"FunctionC");
functionList.Items.Add(
"FunctionD");

Alright, so now we know that depending on which item is selected, we want to call one of our four functions. Since they all have the same interface (and they have to to use a delegate) we simply define a delegate that matches them. This delegate can be privately scoped inside your class.

delegate
void FunctionDelegate();

And then we create an instance variable to hold the current function pointer we want to use:

private FunctionDelegate m_functionPointer =
null;

We add an event handler for the SelectedIndexChanged event of the ListBox (in the Form constructor)

functionList.SelectedIndexChanged += new EventHandler(functionList_SelectedIndexChanged);

And implement the event handler. It simply looks at the newly selected index in the list and changes the value stored in m_functionPointer appropriately.

void functionList_SelectedIndexChanged(object sender, EventArgs e)
{
// determine which function pointer to store based on selection
switch (functionList.SelectedIndex)
{
case 0:
m_functionPointer
= FunctionA;
break;
case 1:
m_functionPointer
= FunctionB;
break;
case 2:
m_functionPointer
= FunctionC;
break;
case 3:
m_functionPointer
= FunctionD;
break;
default:
m_functionPointer
=
null;
break;
}
}

Next we wire up an event handler for our button (again the the Form constructor):

callButton.Click += new EventHandler(callButton_Click);

And finally the magic and simplicity of the state-dependent call

void callButton_Click(object sender, EventArgs e)
{
// call our function (as long as it's not null)
if (m_functionPointer !=
null)
{
m_functionPointer();
}
}

That's all there is to it. Run the application, select a function and click the button.


Get the full source here.

Source: Chris Tacke

No comments: