frenchie
frenchie

Reputation: 51917

implementing delegates in c#

This would be the first time I'd use delegates in c# so please bear with me. I've read a lot about them but never thought of how/why to use this construct until now.

I have some code that looks like this:

public class DoWork()
{

   public MethodWorkA(List<long> TheList) {}

   public void MethodWork1(parameters) {}

   public void MethodWork2(parameters) {}

}

I call MethodWorkA from a method outside the class and MethodWorkA calls MethodWork 1 and 2. When I call methodA, I'd like to pass some sort of parameter so that sometimes it just does MethodWork1 and sometimes it does both MethodWork1 and MethodWork2.

So when I call the call it looks like this:

DoWork MyClass = new DoWork();
MyClass.MethodA...

Where does the delegate syntax fit in this?

Thanks.

Upvotes: 0

Views: 244

Answers (3)

Tigran
Tigran

Reputation: 62248

Considering that all methods are inside the same class, and you call MethodWorkA function using an instance of the class, I honestly, don't see any reason in using Action<T> or delegate, as is I understood your question.

When I call methodA, I'd like to pass some sort of parameter so that sometimes it just does MethodWork1 and sometimes it does both MethodWork1 and MethodWork2.

Why do not just pass a simple parameter to MethodWorkA, like

public class DoWork()
{

   public enum ExecutionSequence {CallMethod1, CallMethod2, CallBoth};
   public MethodWorkA(List<long> TheList, ExecutionSequence exec) 
   {
       if(exec == ExecutionSequence.CallMethod1) 
         MethodWork1(..);
       else if(exec == ExecutionSequence.CallMethod2)
         MethodWork2(..);
       else if(exec == ExecutionSequence.Both)
       {
          MethodWork1(..);
          MethodWork2(..);
       } 
   }

   public void MethodWork1(parameters) {}

   public void MethodWork2(parameters) {}

}

Much simplier and understandable for your class consumer.

If this is not what you want, please explain.

EDIT

Just to give you an idea what you can do:

Example:

public class Executor {

     public void MainMethod(long parameter, IEnumerable<Action> functionsToCall) {
            foreach(Action action in functionsToCall) {
                  action();
            }
     }
}

and in the code

void Main()
{
    Executor exec = new Executor();
    exec.MainMethod(10, new List<Action>{()=>{Console.WriteLine("Method1");}, 
                                         ()=>{Console.WriteLine("Method2");}
    });
}

The output will be

Method1
Method2

In this way you, for example, can push into the collection only functions you want to execute. Sure, in this case, the decision logic (which functions have to be executed) is determined outside of the call.

Upvotes: 0

ispiro
ispiro

Reputation: 27633

For what you described, you don't need delegates.

Just do something like this:

public class DoWork
{
    public void MethodWorkA(List<long> theList, bool both) 
    {
        if (both)
        {
            MethodWork1(1);
            MethodWork2(1);
        }
        else MethodWork1(1);
    }

    public void MethodWork1(int parameters) { }

    public void MethodWork2(int parameters) { }
}

If you're just experimenting with delegates, here goes:

public partial class Form1 : Form
{
    Func<string, string> doThis;

    public Form1()
    {
        InitializeComponent();
        Shown += Form1_Shown;
    }

    void Form1_Shown(object sender, EventArgs e)
    {
        doThis = do1;
        Text = doThis("a");
        doThis = do2;
        Text = doThis("a");
    }

    string do1(string s)
    {
        MessageBox.Show(s);
        return "1";
    }

    string do2(string s)
    {
        MessageBox.Show(s);
        return "2";
    }
}

Upvotes: 0

SLaks
SLaks

Reputation: 887195

public void MethodWorkA(Action<ParamType1, ParamType2> method) {
    method(...);
}

You can call it using method group conversion:

MethodWorkA(someInstance.Method1);

You can also create a multicast delegate that calls two methods:

MethodWorkA(someInstance.Method1 + someInstance.Method2);

Upvotes: 3

Related Questions