Should I invoke the delegate multiple times to pass different parameters to same method?
I have a method to do something based on the input parameter. Now, I want to call the method by adding to the invocation list of a delegate but with different parameters.
is it possible to do without invoking the delegate multiple times?
private delegate void myDel(int a);
private myDel del;
public MainWindow()
{
InitializeComponent();
del = delmethod;
}
private void delmethod(int a)
{
//Do something
}
private void call_methods()
{
del(1);
del(2);
del(3);
}
Is this the right way or I have any other options? Please note that , I may want to pass many parameters like this using a loop.
though I referred here, they have solution only for calling different methods but I want for the same method.
E开发者_运维百科DIT :
I want to transfer data from one database to another by calling the method depending on the parameter
Environment : Windows forms, .Net 3.5
by adding to the invocation list of a delegate but with different parameters.
Possible but ugly. You end up with something like:
del += (x) => delmethod(1);
del += (x) => delmethod(2);
del += (x) => delmethod(3);
del(-1); // Note the -1 is not used
I would go for
private void delmethod(params int[] abc)
{
//Do something for each item in abc
}
private void call_methods()
{
del(1,2,3); //you can call with any number of parameters
}
精彩评论