Hello, HBMSGuy.
Are you sure, that you have error about passing derived class when method asks the base one? Only thing, that I see it's a few mistakes (in other area):
Class TaskToRun
{
public virtual void run ();
}
here if you don't specify any functionality to the method 'run', call the calss (also as this method) as abstract. Or add empty brackets pair: { }
- if you want to leave it as virtual.
Class SayHi : TaskToRun
{
public run()
{
Console.WriteLine("Hi");
}
}
if you decided to make your base class abstract, you should add 'override' keyword to method run
. Also the return type is also needed :P
if you decided to leave your method in base class as virtual - you have 2 ways to go:
Warning 'SayHi.run()' hides inherited member 'TaskToRun.run()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
RunTask(TaskToRun task)
{
delegate void MethodToRun();
MethodToRun methodtorun = new MethodToRun(task.run);
methodtorun();
}
The delegate declaration should be at class level (like you would declare variable in the class) .. or higher - in namespace. You decide.
Also there're some notes about initializing and calling it. Look here, there is good example: delegate (C# Reference).
Well, here's what we have in result (my version):
delegate void MethodToRun();
class TaskToRun
{
public virtual void run() { }
}
class SayHi : TaskToRun
{
public override void …