[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Pass Method in Parameter

Hi,

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;            
            PerformReceiveOnRepository(repository);
        }

        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) =>
            {
                manualResetEvent.WaitOne();
                if (statusResult.Error) showError(statusResult);
                else
                {
                    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;
                    //nextMethod());  
                }
            });
        }

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?
0
zattz
Asked:
zattz
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
        {
            Console.WriteLine("Foo()");
            f2(msg);
        }

        private delegate void Foo2Delegate(string msg);

        private void Foo2(string msg)
        {
            Console.WriteLine("Foo2(): " + msg);
        }

Open in new window

0
 
zattzAuthor Commented:
I'm still lost as how I would apply that to my above code
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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).
0
 
zattzAuthor Commented:
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
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now