Dim Methods as Dictionary
Methods.Value("CallMyMethod") = AddressOf MyMethod
Methods.Value("CallAnotherMethod") = AddressOf MyOtherMethod
Dim s as string = "CallMyMethod"
Methods.Value(s).invoke
Using your example as a reference. If I created a method called “MyThirdMethod”, am I correct in saying I wouldn’t be able to invoke that without first adding it to the dictionary? e.g.
Correct. I usually have a method called “LoadDelegates” or some such that I call when the window or app opens, where the dictionary is loaded with all the methods I might want to invoke.
To answer you directly, Kem. I have a Timer that is triggered when a SQL query takes place within a Thread. The Timer checks to see if the Thread has completed and if so calls a Method that is specified as a parameter in the original Method. Generally the ending Method will be where the UI is updated (if needed).
In short, I guess I’m trying to write as little code as possible to keep things as simple as possible.
With Julia’s example I can achieve this, I guess I was just hoping for a very simple “runAsMethod(String)” that would process the entire thing without the need to link String to Method up front. Call it laziness I guess
If this is the only thing his timer does, it could enable the removal of such the timer. But if such delegate involves access to the UI, it adds another class of complexity just to fire it (Thread.UserInterfaceUpdate) or firing another timer from it.
won’t compile. You need to cast the variant from the dictionary as a delegate before you can invoke the method.
Add a delegate to your module or window or class, using the Insert menu of the IDE. Once you’ve created a delegate and called it, say, “MyMethodCaller”, cast the variant like this:
MyMethodCaller(Methods.Value(s)).Invoke
I don’t think you can declare a delegate locally with Var or Dim, I think you have to insert it with the IDE, but I could be wrong about that.