Another Noob Question: Variable Method Call?
Is it possible to select a method called by using a global variable?
I would like to do something like this:
VAR
BYTE MenuItem1 [7]
PUB
MenuItem1 := Method1
MenuItem1
And have Method1 (or whatever method I make equal MenuItem1 run. But I get back "variable needs an operator." What am I missing here, please? Any suggestions would be much appreciated.
I would like to do something like this:
VAR
BYTE MenuItem1 [7]
PUB
MenuItem1 := Method1
MenuItem1
And have Method1 (or whatever method I make equal MenuItem1 run. But I get back "variable needs an operator." What am I missing here, please? Any suggestions would be much appreciated.
Comments
if I understand reight you want something like dependant on the value of a variable executing different methods.
therefore you can use a construction like this:
VAR long MethodNr Pub MyMethod_choosing case MethodNr 1 : Method1 2 : Method2 3 : Method3 Pub Method1 '.... Pub Method2 '.... Pub Method3 '....
if you want to do something else please tell in more details what you want to do.
best regards
Stefan
Thanks for the response. No, I am actually wanting to call a method by storing the name of the method to be called inside a variable array and then call the method by retrieving the name from the variable array.
My application is to be able to customize what method(s) are called from a menu (each menu item calling a different method) without changing the menu code, just the variable array values to choose what methods I want to be triggered by the menu items.
Perhaps another way of looking at it is that I would like to be able to have an "alias" for a method and call it instead of the method name itself.
If you are coming from c/c++ and you are looking for something like method-pointers this is not possible in standard spin.
Some members here in the forum have developed tricky ways to something similar like method-pointers.
I'm not familiar with this and especially not familiar with method-pointers in SPIN
here is the link to that thread http://forums.parallax.com/showthread.php?128397-Callbacks-and-Method-Pointers-in-Spin&highlight=Method-Pointers
best regards
Stefan
Could you please explain a bit more what you mean? Are you saying there is some other way to call a method using constants?
CON c_MyMethodA = 1 c_MyMethodB = 2 c_MyMethodC = 3 VAR long MethodNr PUB Main '..do several things if ... MethodNr := c_MyMethodB if ... MethodNr := c_MyMethodA if ... MethodNr := c_MyMethodC MyMethodHandler(MethodNr) PUB MyMethodHandler(p_MethodNumber) case p_MethodNumber c_MyMethodA : Method_A c_MyMethodB : Method_B c_MyMethodC : Method_C PUB Method_A '... PUB Method_B '... PUB Method_C '...
best regards
Stefan