简体   繁体   中英

Why does my delegate not work when passed in as a parameter to a method?

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);
        }
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM