Click here to Skip to main content
15,914,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,
I want to make delegate following this method like this way but it won't work.
C#
private delegate void CopyFileDelegate(string srcDir, string DesDir);

public void CopyFile(string srcDir, string DesDir)
{
    if (txtLoadPath.InvokeRequired == false)
    {
    }
    else
    {
        txtLoadPath.BeginInvoke(new CopyFileDelegate(CopyFile(srcDir, DesDir)), new object[] {});  // Here is the problem
    }
}

error said method needed.
what's wrong here, what should I do. Please help me. I am new in delegate.
Mahmud
Posted
Updated 11-Oct-10 1:30am
v4

You don't specify parameters when creating an instance of a delegate. Look here for some examples of what you are trying to do.
 
Share this answer
 
You could try this -
CopyFileDelegate objD = new CopyFileDelegate(CopyFile);
txtLoadPath.BeginInvoke(objD, new object[] { srcDir , DesDir});
 
Share this answer
 
Your txtLoadPath.BeginInvoke accepts a delegate of CopyFileDelegate type as the first parameter. You create an instance of this delegate incorrectly.
Instead of:

<br />
new CopyFileDelegate(CopyFile(srcDir,DesDir))


please write:

<br />
new CopyFileDelegate(CopyFile)


Your code should be:

C#
private delegate void CopyFileDelegate(string srcDir,string DesDir);
       public void CopyFile(string srcDir,string DesDir)
       {
           if (txtLoadPath.InvokeRequired == false)
           {
           }
           else
            {
        txtLoadPath.BeginInvoke(new CopyFileDelegate(CopyFile), new object[] {});
            }
       }
 
Share this answer
 
Comments
Abhinav S 10-Oct-10 1:51am    
I'm a little confused - you are missing the two CopyFile parameters right?
Ed Guzman 10-Oct-10 10:40am    
Correct, when you instantiate a delegate (using new keyword), you just point to the name of the function without parameters
Please read my article
<a href="http://www.codeproject.com/KB/cs/delegatespart1.aspx">http://www.codeproject.com/KB/cs/delegatespart1.aspx</a>
AspDotNetDev 10-Oct-10 13:50pm    
And how do you expect the parameters to be passed to the function? This will compile, but it will not work.
Ed Guzman 10-Oct-10 15:11pm    
Actually the first parameter of the TextBox.BeginInvoke method is System.Delegate, and according to Microsoft this is a delegate to a method that takes no parameters.
AspDotNetDev 11-Oct-10 11:42am    
I never said the parameters should be passed via the first parameter of BeginInvoke. CopyFileDelegate can be used in place of type Delegate. The second parameter of BeginInvoke is the array of parameters that will be passed to the first parameter. What did you think it was for and why would it be there if the only overload for this method ignored that array? Like I said, your method will compile, but it will not function correctly, because it's dropping off parameters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900