Main Content

Combine and Remove .NET Delegates

MATLAB® provides the instance method Combine, that lets you combine a series of delegates into a single delegate. The Remove and RemoveAll methods delete individual delegates. For more information, refer to the documentation for a .NET class library, as described in To Learn More About .NET.

For example, create the following MATLAB functions to use with the NetDocDelegate.delInteger delegate:

function out = action1(n)
out = "Add flour";
function out = action2(n)
out = "Add eggs";

Create delegates step1 and step2:

step1 = NetDocDelegate.delInteger(@action1);
step2 = NetDocDelegate.delInteger(@action2);

To combine into a new delegate, mixItems, type:

mixItems = step1.Combine(step2);

Or, type:

mixItems = step1.Combine(@action2);

Invoke mixItems:

result = mixItems(1);

In this case, the function action2 follows action1:

Add flour
Add eggs

The value of result is the output from the final delegate (step2).

result = 
Add eggs

You also can use the System.Delegate class static methods, Combine, Remove, and RemoveAll.

To remove a step1 from mixItems, type:

step3 = mixItems.Remove(step1);

See Also

| |

Related Topics