4

I have below code

    StringOperations sumString, reverseString, lowerString, upperString, multicastString;

    sumString = new StringOperations(sum);
    reverseString = new StringOperations(reverse);
    lowerString = new StringOperations(lower);
    upperString = new StringOperations(upper);

    multicastString = upperString + lowerString + reverseString + sumString;

    int count = 4;

    if (!checkBox1.Checked)
    {
            multicastString -= upperString;
            count--;
    }
    if (!checkBox2.Checked)
    {
            multicastString -= reverseString;
            count--;
    }
    if (!checkBox3.Checked)
    {
            multicastString -= lowerString;
            count--;
    }
    if (!checkBox4.Checked)
    {
            multicastString -= sumString;
            count--;
    }
    if (count > 0)
    {
            string test = multicastString(textBox1.Text);
    }

When uppercase and lowercase checkboxes are selected then it only show me lowercase function's result.

If I select uppercase, lowercase and reverse checkboxes then it only show me result of reverse function.

My delegate is below

delegate string StringOperations(string str);

I am using multicast delegate and returning string as shown in above code. Please let me know what I am doing wrong?

TaW
  • 53,122
  • 8
  • 69
  • 111
Junaid
  • 2,572
  • 6
  • 41
  • 77

2 Answers2

2

When you have a delegate that has multiple handlers attached to it, you will still get only one return value. There is no direct way to get the other values and naturally you cannot chain the handler functions in a way that the return value of one would be sent to another. The only thing you will get is the last attached handler's return value is returned.

There is no ambiguous behaviour here really, it's just the way it works. If you want to chain the functions you have to use a different approach then a delegate. In this example you could just call the functions and that's it.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Well I changed the return type to `void` and do all computations in the methods and display result with methods. – Junaid Aug 16 '15 at 12:29
  • @Junaid The normal way would be just to call the methods without any use of delegates. Then you could chain them however you want. Delegates have their place, but this is not one of them. – Sami Kuhmonen Aug 16 '15 at 12:30
  • Can you please let me know when to use delegate and when not ? Is there any useful link that you can share? Thanks – Junaid Aug 16 '15 at 12:41
  • 1
    @Junaid Most common use for them are events, where any compliant object can subscribe for the event and get the delegate called when something happens. Usually there is no return value in this case. In this code you know what functions to call, in which order and you want something in return. Therefore there is no need to use delegates since you can just call the functions in order and use the return value as the argument for the next one. If the method was not known and could change, then there might be a case for single cast delegate with a return value. – Sami Kuhmonen Aug 16 '15 at 12:45
0

In multicast delegate, methods should have void as return type because it will not mix the values. So method signature will change

from

delegate string StringOperations(string str);

to

delegate void StringOperations(string str);

PS: Also change the return type of other delegated methods to void.

Junaid
  • 2,572
  • 6
  • 41
  • 77