Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to store functions and arguments to execute later

Suppose I have a 3 functions:

void A(int i){...}
void B(bool b, string s){...}
void C(float f, double d, char c){...}

In my application, in a given moment, I open a popup that has a button. My popup has a controller (called PopupController). It has references for everything in the popup and includes the function OnButtonPress(), called by the button at pressing.

My question is how to make the popup button execute those 3 functions with given parameter?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...
var popupController = PopupController.CreateInstance();
//I wanna execute this only at button press:
A(1);
B(true, "s");
C(1, 2, 'c');

How can I do this? I’m allowed to create whatever is needed to store these functions in the PopupController.

>Solution :

delegates; for example

Action thing = () => A(1);

or

EventHandler handler = delegate {
    A(1);
    B(true, "s");
    C(1, 2, 'c');
};

then invoke that delegate later when needed, for example thing(); or handler(this, EventArgs.Empty);.

this is even easier if the button-press is itself an event:

btn.Click += delegate {
    A(1);
    B(true, "s");
    C(1, 2, 'c');
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading