Solved

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

Posted on 2012-03-29
9
219 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

23 Experts available now in Live!

Get 1:1 Help Now