I have an algorithm that “always” runs steps A, C, D in that order.
I want to allow a way to run A, B, C, D, where B can be multiple methods. C, D “should” be unaffected, but I cannot parallelize it. It is just writing to a file.
Any way, I know a tiny bit about delegates and have used the AddressOf to hook up an event handler.
So I was thinking of simply creating an array or collection of what not of “addresses” or delegates.
Is this even possible?
Is this the ‘best’ way? I know how to force the behavior with objects, but I am hoping to reuse any mechanisms already built in to VB, .NET.
Dane
You could simply use an event to store and invoke multiple methods. Events are basically streamlined multicast delegates, so you won’t have to use
Delegate.Combine. The drawback is that your methods won’t be able to have return types.Here’s an alternative way to use multicast delegates, as well. This example assumes you want a value to be returned from the methods. If you don’t need this, you won’t have to loop through the methods; you can just use
myMethods.Invoke.As another alternative to that invocation code above, you could use the
Delegate.DynamicInvokemethod to not have to cast the delegate. That, however, would use late-binding, and, in the case of a function, would require the return value to be cast anyway.