Pass Method in Parameter

Posted on 2012-09-11
Last Modified: 2012-09-13

I have never used delegates before and I am totally lost in the documentation.

Here is my code:

public static void DownloadAssetGroups()
            IBaseRepository repository = (activity.Application as AssetManagerApplication).AssetGroupRepository;            

        private static void PerformReceiveOnRepository(IBaseRepository repository)
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            ReceiveStatusResult statusResult = new ReceiveStatusResult();
            ThreadPool.QueueUserWorkItem((s) => ReceiveClass.Receive(manualResetEvent, activity, repository, statusResult));

            ThreadPool.QueueUserWorkItem((s) =>
                if (statusResult.Error) showError(statusResult);
                    resultToastMessage += (activity.Application as AssetManagerApplication).AssetGroupRepository.GetObjectTypeClassDisplayNamePlural() + " Downloaded " + statusResult.DownloadCount.ToString() + " New " + statusResult.NewCount.ToString() + " Modified " + statusResult.ModifiedCount.ToString() + "" + System.Environment.NewLine + System.Environment.NewLine;

Open in new window

Basically I call DownloadAssetGroups() which calls PerformReceiveOnRepository.

However, at the end of the repository I want to call another method, on the line I've commented on that says // nextMethod(), I want to pass the nextMethod into the PerformReceiveOnRepository method

Can anybody help?
Question by:zattz
    LVL 85

    Accepted Solution

    Basically you create a delegate that has the same signature as your target method.  *The delegate can be used to "point" to any method that has that same signature.  Then you create an instance of that delegate that points to your target method.  You can declare a parameter of that type delegate to pass into your method.

    A simple example:
            private void button1_Click(object sender, EventArgs e)
                Foo(new Foo2Delegate(Foo2), "Hello Foo2!");
            private void Foo(Foo2Delegate f2, string msg)
            private delegate void Foo2Delegate(string msg);
            private void Foo2(string msg)
                Console.WriteLine("Foo2(): " + msg);

    Open in new window


    Author Comment

    I'm still lost as how I would apply that to my above code
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    What's the signature for your target method?  Start by making a delegate that has that same signature.  Then add a parameter to your existing PerformReceiveOnRepository() method so it can receive another parameter of your new delegate type (along with any other pertinent parameters).  Finally, at the end of PerformReceiveOnRepository(), simply execute your delegate (passing parameters as necessary).

    Author Closing Comment

    I got it to work by trial and error. I still need to take the time to understand delegates, but I was under a tight deadline. Thank you very much

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now