Multicast delegate ambiguous

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); } 

If the upper and lower case flags are selected, this only displays the result of the bottom line.

If I select uppercase, lowercase, and reverse flags in upper case, then it will show me the result of the inverse function.

My delegate is below

 delegate string StringOperations(string str); 

I use the multicast delegate and return a string as shown in the code above. Please let me know what I am doing wrong?

+4
source share
2 answers

When you have a delegate that has several handlers attached to it, you will only get one return value anyway. There is no direct way to get other values, and, of course, you cannot bind handler functions so that the return value of one is sent to the other. The only thing you get is to return the last returned return value of the handler.

There is no ambiguous behavior; it is just the way it works. If you want to bind functions, you need to use a different approach and then a delegate. In this example, you can just call functions and what it is.

+2
source

In multicast deletes, methods must have void as the return type, because it will not mix values. Thus, the signature of the method 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 .

0
source

All Articles