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.![Smile :) :)](/styles/default/custom/smilies/smile.gif)
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
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.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
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