way to update a variable in BaseInputDelegate from its own Submenu Delegate without using a global variable?

Hello.  Lets say I have a BaseInputDelegate, and a Submenu with its own Delegate, which in Monkey C, each have to be separate classes.  Lets say I want the Submenu to adjust a variable that will be used in the rest of the regular BaseInputDelegate.  Do I need to use a global var or can I somehow use some tricky programming logic to accomplish this?  

The separation in the classes makes me think that it is impossible to somehow do it, without the use of a global variable.  Please explain if there is another way! thanks!

var gSelectButtonLocked; // do I really need this, or is there a better way?

class BaseInputDelegate extends WatchUi.BehaviorDelegate
{

function onSelect() {
   if (gSelectButtonLocked!= true) { // if buttons are unlocked
      // process normal Select button functionality, i.e., start or stop activity
   } 
}

function onMenu() {
   var menu = new WatchUi.Menu();
   var delegate;
   menu.setTitle("My Menu");
   menu.addItem("Lock Buttons", :one);
   menu.addItem("Another Option", :two);
   delegate = new MyMenuDelegate(); 
   WatchUi.pushView(menu, delegate, WatchUi.SLIDE_IMMEDIATE);
   return true;
}

} // end of class BaseInputDelegate -----------------------------------

class MyMenuDelegate extends WatchUi.MenuInputDelegate {

function onMenuItem(item) {
   if (item == :item_1) {
      System.println("User wants to lock buttons");
      gSelectButtonLocked = !gSelectButtonLocked;
   } else if (item == :item_2) {
      System.println("Item 2");
   }
}

} // end of class MyMenuDelegate --------------------------------------

  • One way is to pass reference to your BaseInputDelegate instance as parameter in constructor of MyMenuDelegate, and then access selectButtonLocked via that:

    class BaseInputDelegate extends WatchUi.BehaviorDelegate
    {
      var selectButtonLocked = false;
    
      function onMenu() {
        ...
        delegate = new MyMenuDelegate(self); 
        WatchUi.pushView(menu, delegate, WatchUi.SLIDE_IMMEDIATE);
        ...
      }
    }
    
    class MyMenuDelegate extends WatchUi.MenuInputDelegate {
      var myDelegate = null;
    
      function initialize(data) {
        myDelegate = data;
      }
    
      function onMenuItem(item) {
        if (item == :item_1) {
          myDelegate.selectButtonLocked = !myDelegate.selectButtonLocked;
        }
      }
    }

    You can also just pass selectButtonLocked variable  in constructor, or use method() function to pass one callback function (which could do the variable update). You can also add setter for the variable, if you want to keep data private.