5

I have an issue that I wish to add function calls to a delegate, but each of these function calls will have a unique parameter. I cannot figure it out or find a solution elsewhere so I am turning to you guys :)

Some pseudo below..

(So basically, I am creating a delegate and an event and the AddToDelegate function is supposed to add the function calls to the event (with a unique value), then the GetData function returns all the responses in one string - the problem comes in the AddToDelegate function as the line a += new A(SomeFunc)(i.ToString()); should really only be a += new A(SomeFunc);)

Is there a way to do this with delegates - or am I barking up the wrong tree?

public delegate string A(string s);
public event A a;

public void AddToDelegate()
{
    for (int i = 0; i < DelegateList.Length; i++)
    {
        a += new A(SomeFunc)(i.ToString());
    }
}

public string GetData()
{
    StringBuilder _sb = new StringBuilder();
    if (a != null)
    {
        Delegate[] DelegateList = a.GetInvocationList();
        for (int i = 0; i < DelegateList.Length; i++)
        {
            _sb.Append(((A)DelegateList[i]));
        }
    }
    return _sb.ToString();
}
Falcon165o
  • 2,875
  • 1
  • 20
  • 31
T1gg3rB0unC3
  • 151
  • 1
  • 10
  • 3
    What are you trying to accomplish? – Allon Guralnek Jul 12 '12 at 13:36
  • The pseudo above would produce a string which details all the unique values – T1gg3rB0unC3 Jul 12 '12 at 13:58
  • The question asks what you are trying to accomplish, not what the above pseudo-code fails to accomplish. You may indeed be barking up the wrong tree but we can't know it unless you tell us *what* you want to do, rather than *how* you are unable to do it. What are your inputs and what are your expected outputs? Give a usage example, rather than an implementation example. – Allon Guralnek Jul 12 '12 at 14:04

2 Answers2

3

Not sure what you really want, but you can use an anonymous function that will hold this extra variable inside its scope:

a += new A( s => { 
    string extra_value = i.ToString();
    return SomeFunc(s, extra_value);
  });

Which can be simplified into this:

a += s => SomeFunc(s, i.ToString());
linepogl
  • 9,147
  • 4
  • 34
  • 45
0

Yes you can do this via labda expressions and closures.

The example below will create add delegates to a that will call SomeFunc with the value of i at the time of the loop. The variable capture is needed to ensure that the correct value is captured - see How to tell a lambda function to capture a copy instead of a reference in C#?

public event Action<string> a;

public void AddToDelegate()
{
    for (int i = 0; i < DelegateList.Length; i++)
    {
        int capture = i;
        a += () => SomeFunc(capture.ToString());
    }
}

public string GetData()
{
    StringBuilder _sb = new StringBuilder();
    if (a != null)
    {
        Delegate[] DelegateList = a.GetInvocationList();
        for (int i = 0; i < DelegateList.Length; i++)
        {
            _sb.Append((Action<string>)DelegateList[i]());
        }
    }
    return _sb.ToString();
}
Community
  • 1
  • 1
shf301
  • 31,086
  • 2
  • 52
  • 86