Solved

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

Posted on 2012-03-29
9
217 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
  • 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 74

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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 74

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 74

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

19 Experts available now in Live!

Get 1:1 Help Now