Reputation:
I created a delegate and i want to use the delegate as a parameter in a method parameter list. I think want to call handler just like I did in the Main method which work perfectly.
Question: How can I pass a delegate to a method?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d) <--Write Here!!!
{
d.handler("33");
}
static void Main(string[] args)
{
// Instantiate the delegate.
// Del handler = DelegateMethod;
Program p = new Program();
p.handler("Hello World"); <--Like this example here (these work)
p.handler("DisneyLand");
p.handler("Cattle Wars");
testDel(p.handler("Cattle Wars");
}
}
}
Error List:
Error 1 The best overloaded method match for 'ConsoleApplication1.Program.testDel(ConsoleApplication1.Program.Del)' has some invalid arguments C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 12 ConsoleApplication1
Error 2 Argument 1: cannot convert from 'void' to 'ConsoleApplication1.Program.Del' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 20 ConsoleApplication1
Error 3 ) expected C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 44 ConsoleApplication1
Working Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d)
{
d.Invoke("L");
}
static void Main(string[] args)
{
// Instantiate the delegate.
// Del handler = DelegateMethod;
Program p = new Program();
p.handler("Hello World");
p.handler("DisneyLand");
p.handler("Cattle Wars");
p.testDel(p.handler);
}
}
}
Upvotes: 4
Views: 115
Reputation: 3213
You have to pass the delegate into the method with out the "Cattle Wars" parameter
testDel(p.handler);
Then you method should look like the following.
public void testDel(Del d)
{
d.Invoke("L");
}
I will have to say that delegates still seem strange to me. I know that they are just variables that are type safe and thread safe. You can use the variable just like calling the method name.
public delegate void Del(string e);
Del handler = DelegateMethod;
In your example here your delegate can be set to any method that has a return type of void and takes one argument that is a string.
Del handler = DelegateMethod;
The above is where you declare you delegate but you also can declare the delegate to any methods that excepts void and one parameter as a string.
handler = BaseBall;
public void BaseBall(string a)
{
//code here
}
I would just keep reading you will learn as you go.
Upvotes: 1
Reputation: 56536
In the line
testDel(p.handler("Cattle Wars"));
p.handler
is invoked. There is no longer a Del
to pass into testDel
. Maybe you were looking for:
testDel(p.handler);
This passes the Del
so that testDel
can invoke it.
Upvotes: 6