Link to home
Start Free TrialLog in
Avatar of dominicwong
dominicwong

asked on

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

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.

}
Avatar of nishant joshi
nishant joshi
Flag of India image

use delegates...which is helpful to work like a function pointer.

Regards,
nishant
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

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.
Avatar of dominicwong
dominicwong

ASKER

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.
Try the solution given by me in my previous post....
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".
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Kaufmed.