Solved

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

Posted on 2012-03-29
9
221 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 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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