Function Pointer in Class

R

Robert Morley

I'm using Visual C# Express 2008 and I'm trying to implement a function
pointer as a member of a class. It sounds like I'm looking for a delegate,
but I don't quite understand what I need to do. As you can perhaps guess,
I'm relatively new to both C# and .NET in general.

The relevant function will be a void with no parameters. My current class
looks like this:

public class MenuItem : Object
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

//public delegate? void FunctionToCall() would go here

public override string ToString()
{
return Name;
}
}

Not very exciting, is it? There's no constructor because a) This is my
first class of any significance and I haven't looked up how to do that yet,
and b) I want the function as part of the constructor if possible, so
implementation will be dependent on the answer I get in any event. :)

The idea is to have an easily-extensible listbox with a variety of functions
to call and you just double-click on whatever it is you want to do. When
MenuItem is instantiated, I would pass in the name of the function to
execute, like:

MenuItem mi = new MenuItem("Descriptive Name", MyFunction());

Then later in the code, I need to be able to do:

mi.FunctionToCall();

How do I go about doing this? Or if I'm on the wrong track, how do I do
something like it?


Thanks,
Rob
 
T

Tom Shelton

I'm using Visual C# Express 2008 and I'm trying to implement a function
pointer as a member of a class. It sounds like I'm looking for a delegate,
but I don't quite understand what I need to do. As you can perhaps guess,
I'm relatively new to both C# and .NET in general.

The relevant function will be a void with no parameters. My current class
looks like this:

public class MenuItem : Object
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

//public delegate? void FunctionToCall() would go here

public override string ToString()
{
return Name;
}
}

Not very exciting, is it? There's no constructor because a) This is my
first class of any significance and I haven't looked up how to do that yet,
and b) I want the function as part of the constructor if possible, so
implementation will be dependent on the answer I get in any event. :)

The idea is to have an easily-extensible listbox with a variety of functions
to call and you just double-click on whatever it is you want to do. When
MenuItem is instantiated, I would pass in the name of the function to
execute, like:

MenuItem mi = new MenuItem("Descriptive Name", MyFunction());

Then later in the code, I need to be able to do:

mi.FunctionToCall();

How do I go about doing this? Or if I'm on the wrong track, how do I do
something like it?


Thanks,
Rob

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent ();
}


private void Form1_Load ( object sender, EventArgs e )
{
this.listBox1.Items.Add ( new HelloWorldFunction () );
this.listBox1.Items.Add ( new YouSucketh () );
}

private abstract class FunctionItemBase
{
protected FunctionItemBase ( string name, Action function )
{
this.Name = name;
this.Function = function;
}

public string Name { get; private set; }
public Action Function { get; private set; }

public override string ToString ()
{
return Name;
}
}


private class HelloWorldFunction : FunctionItemBase
{
public HelloWorldFunction () : base ( "Hello World!", () => MessageBox.Show ( "Hello, World!" ) ) { }
}

private class YouSucketh : FunctionItemBase
{
public YouSucketh () : base ( "You Sucketh!", () => MessageBox.Show ( "You Sucketh!" ) ) { }
}

private void listBox1_DoubleClick ( object sender, EventArgs e )
{
FunctionItemBase function = listBox1.SelectedItem as FunctionItemBase;
if ( function != null )
function.Function ();
}

}
}
 
R

Robert Morley

Tom said:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent ();
}


private void Form1_Load ( object sender, EventArgs e )
{
this.listBox1.Items.Add ( new HelloWorldFunction () );
this.listBox1.Items.Add ( new YouSucketh () );
}

private abstract class FunctionItemBase
{
protected FunctionItemBase ( string name, Action function )
{
this.Name = name;
this.Function = function;
}

public string Name { get; private set; }
public Action Function { get; private set; }

public override string ToString ()
{
return Name;
}
}


private class HelloWorldFunction : FunctionItemBase
{
public HelloWorldFunction () : base ( "Hello World!", () => MessageBox.Show ( "Hello, World!" ) ) { }
}

private class YouSucketh : FunctionItemBase
{
public YouSucketh () : base ( "You Sucketh!", () => MessageBox.Show ( "You Sucketh!" ) ) { }
}

private void listBox1_DoubleClick ( object sender, EventArgs e )
{
FunctionItemBase function = listBox1.SelectedItem as FunctionItemBase;
if ( function != null )
function.Function ();
}

}
}

That looks great! Thanks, Tom.


Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top