Solved

How to pass a Callback method together with its parameters to another method.

Posted on 2012-03-29
9
225 Views
Last Modified: 2012-03-30
Hi experts

I wonder how I might pass a method together with its parameters into a second method so that the second method can call that first method (without knowing its parameters).

eg.
public void TopFunc()
{
     byte result = Method1(1, 2, 3);
 
     Method2( Method1 );  //  Pass in Method1 together with its parameters.
}

private byte Method1(byte x, byte y, byte z)
{
    // do something
    return 0;
}

private void Method2(..method1..)
{
    // do something

    if(condition)
       byte result = method1;  // call but using the parameters previously defined in "TopFunc".
                                                 // "Method2" here doesn't know what the parameters are.

}
0
Comment
Question by:dominicwong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37785848
use delegates...which is helpful to work like a function pointer.

Regards,
nishant
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37785851
Here it is:-

public void methodTest()
{
Method2(Method1);
}

public bool Method1(byte b1,byte b2,byte b3)
        {
            return true;
        }

        public bool Method2(Func<byte,byte,byte,bool> myMethod)
        {
            const bool test = true;

            if(test)
            {
                return myMethod(1, 2, 3);
            }
        }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37786397
Can you describe the environment a bit more? What is your overall goal (outside of what is mentioned above)?

Functions don't typically "remember" anything. You say, "call but using the parameters previously defined in 'TopFunc'," but since "TopFunc" has already executed, everything associated with it is gone from memory.

If I have an inkling of what you are trying to do, then you might get away with using static variables to hold the data. Then each function can retrieve the values of the static variables and use them how each sees fit--whether it be internal to the function, or passed as parameters.

As far as passing a function as a parameter, use delegates as already mentioned.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dominicwong
ID: 37786729
Thanks Kaufmed.

My environment is my software had to communicate with an external hardware.

Say, my software had executed "Method1" to send something to the external hardware.
It then executed "Method2". If the external hardware doesn't respond by certain time, "Method2" would excute the previous "Method1" again but with the same parameters.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37786734
Try the solution given by me in my previous post....
0
 

Author Comment

by:dominicwong
ID: 37786762
Thanks BuggyCoder.

I am afraid that won't work in my situation.
"Method1" was first called with some parameters.
"Method2" does not know the parameters that had been passed in to "Method1".
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37786776
Then expanding on BuggyCoder's approach, I would say pass the parameters as well:

public void TopFunc()
{
     byte p1 = 1;
     byte p2 = 2;
     byte p3 = 3;
     
     byte result = Method1(p1, p2, p3);
  
     Method2( Method1, p1, p2, p3);  //  Pass in Method1 together with its parameters.
}

private byte Method1(byte x, byte y, byte z)
{
    // do something
    return 0;
}

private void Method2(Func<byte, byte, byte, bool> method1, byte p1, byte p2, byte p3)
{
    // do something

    if(condition)
       byte result = method1(p1, p2, p3);  // call but using the parameters previously defined in "TopFunc".
                                           // "Method2" here doesn't know what the parameters are.
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 37786789
The static variable approach I mentioned earlier I had envisioned as being something like:

private static byte p1;
private static byte p2;
private static byte p3;

public void TopFunc()
{
     p1 = 1;
     p2 = 2;
     p3 = 3;
     
     byte result = Method1(p1, p2, p3);
  
     Method2( Method1 );  //  Pass in Method1 together with its parameters.
}

private byte Method1(byte x, byte y, byte z)
{
    // do something
    return 0;
}

private void Method2(Func<byte, byte, byte, bool> method1)
{
    // do something

    if(condition)
       byte result = method1(p1, p2, p3);  // call but using the parameters previously defined in "TopFunc".
                                           // "Method2" here doesn't know what the parameters are.
}

Open in new window


The above is not thread-safe, though. That may not matter for your environment.
0
 

Author Closing Comment

by:dominicwong
ID: 37786834
Thanks Kaufmed.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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