Shop OBEX P1 Docs P2 Docs Learn Events
Another Noob Question: Variable Method Call? — Parallax Forums

Another Noob Question: Variable Method Call?

coryco2coryco2 Posts: 107
edited 2011-03-23 13:51 in Propeller 1
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.

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-03-23 09:07
    Hi Coryco2,

    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
  • coryco2coryco2 Posts: 107
    edited 2011-03-23 09:27
    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.
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-03-23 10:43
    Aha. A way in standard SPIN for that is to define constants and code one big method-handler with a case statement.

    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
  • coryco2coryco2 Posts: 107
    edited 2011-03-23 11:22
    Aha. A way in standard SPIN for that is to define constants and code one big method-handler with a case statement.

    Could you please explain a bit more what you mean? Are you saying there is some other way to call a method using constants?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-03-23 12:59
    There is no standard way to call a method from a variable as you describe. The best standard technique is to use a case statement like Stefan suggested. You could also use the Method Pointer object that I posted in the OBEX. It is located at http://obex.parallax.com/objects/697/ .
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-03-23 13:40
    this is the basic structure of using constants and a method handler
    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
  • coryco2coryco2 Posts: 107
    edited 2011-03-23 13:51
    So, you are essentially assigning each method a constant number (cMyMethodA=1, etc.) , then IF or CASE testing to see if that number matches whatever value you give variable "MethodNr" and if it does it calls the corresponding PUB method? Hmm...that could work. Thanks.
Sign In or Register to comment.