?
Solved

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

Posted on 2012-03-29
9
Medium Priority
?
226 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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