Pointer like approach / variable as parameter

Hello,

du to the fact, that Ui.NumberPicker and Ui.Confirmation are only working with delegates, it is not possible to store the selected result into a variable.
Okay, it is. But only into a single variable.

What I´ve got:
class Settings
{
var Value1 = 1;
var Value2 = 5;
var Value3 = false;
}


Now I need two seperate NumberPicker Delegates for Value1 and Value2:
class NpVal1 extends Ui.NumberPickerDelegate
{
function onNumberPicked(value)
{
Settings.Value1 = value;
}
}
class NpVal2 extends Ui.NumberPickerDelegate
{
function onNumberPicked(value)
{
Settings.Value2 = value;
}
}


Also two different calls:
var np = new Ui.NumberPicker( Ui.NUMBER_PICKER_DISTANCE, Settings.Value1 );
Ui.pushView( np, new NpVal1(), Ui.SLIDE_IMMEDIATE );

var np = new Ui.NumberPicker( Ui.NUMBER_PICKER_DISTANCE, Settings.Value2 );
Ui.pushView( np, new NpVal2(), Ui.SLIDE_IMMEDIATE );


That is really annoying, to have several functions and calls, which nearly do the same. If Monkey C have had pointer, I could submit Settings.Value1 or Settings.Value2 to the delegate, and I´d be fine.

Is there a nicer solution?
  • Yes, there are a nicer solutions. You just need to make your delegate generic. Note that I'm writing untested code here, but it should work.

    class MyNumberPickerDelegate extends Ui.NumberPickerDelegate
    {
    hidden var _M_object;
    hidden var _M_symbol;

    function initialize(object, symbol) {
    NumberPickerDelegate.initialize();
    _M_object = object;
    _M_symbol = symbol;
    }

    function onNumberPicked(value) {
    // this is a little-known trick to access a member
    _M_object[_M_symbol] = value;
    }
    }

    // now use it (assumes you have an object named settings of type Settings)
    var np = new Ui.NumberPicker( Ui.NUMBER_PICKER_DISTANCE, settings.Value1 );
    Ui.pushView( np, new MyNumberPickerDelegate(settings, :Value1), Ui.SLIDE_IMMEDIATE );


    The above is direct access. If you hide your data in the Settings class, you might want to access/modify it via function.

    class Settings
    {
    hidden var _M_value1 = 1;
    hidden var _M_value2 = 5;

    function setValue1(value1) {
    _M_value1 = value1;
    }

    function getValue1() {
    return _M_value1;
    }

    function setValue2(value2) {
    _M_value2 = value2;
    }

    function getValue2() {
    return _M_value2;
    }
    }

    // now use it (assumes you have an object named settings of type Settings)
    var np = new Ui.NumberPicker( Ui.NUMBER_PICKER_DISTANCE, settings.getValue1() );
    Ui.pushView( np, new MyNumberPickerDelegate(settings.method(:setValue1), Ui.SLIDE_IMMEDIATE );


    That said, the NumberPicker class is deprecated. You should probably avoid using it.

    Travis
  • Travis, you made it! Thank you very much!!!

    Nice to know how to work with symbols :)
  • That said, the NumberPicker class is deprecated. You should probably avoid using it.


    Ups, overread it on the first sight:eek:

    Thanks for your help. It´s working, but I´m still on it to make it nicer and more generic.
    Did not have a clue how to create a good boolean-Picker. Until now, I´m taking a ConfirmationDialog.

    Sharing is caring:
    GitHub\GenericPicker.mc
    (Will bee updated next week)
  • For booleans, you could use a standard menu. Make it in your code (a dynamic menu), and not using an xml. When you add items, use addItem(), and the strings could be "Option1 - on", "Option2 - off", etc. and with the menu item is selected, toggle the boolean (the back button would leave things unchanged) You can't use graphics in the menu, just indicate the current state with text.