Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Why do long values in radiobox become int?

    I'm sure i've missed something, but please consider the example below.

    As you can see, i am adding two Long values to the radiogroup, and initializing it with a value.

    However, when i try to get a long value back out it, it doesn't work because the value has become an Int!

    It works if i have clicked on something in the radiogroup, but not if i get it out in its initial state.

    I got around it by putting Stringvalues in the box, and doing String.parseLong, but i'm curious why this is.

    Some input would be interesting. Cheers


    Code:
    public void onModuleLoad(){
            Button button = new Button("Check value");
    
            RadioGroupItem filterType = new RadioGroupItem("type", "Select the thingy");
            filterType.setVertical(false);
            filterType.setTitleOrientation(TOP);
            Map vals = new HashMap<>();
            vals.put(1L, "The first value");
            vals.put(2L, "The second value");
            filterType.setValueMap(vals);
            filterType.setValue(1L);
    
            DynamicForm form = new DynamicForm();
            form.setItems(filterType);
    
            button.addClickHandler(clickEvent -> {
                try {
                    Long val = (Long) filterType.getValue();
                    SC.say("The value is: " + val);
                } catch (Exception e) {
                    SC.say("Error, because the value returned is not a Long, but a " +filterType.getValue().getClass().getName());
                }
            });
    
            Layout l = new VLayout();
            l.addMember(form);
            l.addMember(button);
    
            l.draw();
        }

    #2
    Remember that everything here is actually executing as JavaScript, which has only one numeric data type, "Number", which covers both floating point and integral values, and has an integral range that is more that Java Integer but less than Java Long (see background).

    So, nothing has "happened to" your value - it was represented as a Number and still is.

    If you're actually using Long values where you might need the entire range, see DataSourceField.stringInBrowser for suggested approaches.

    Comment

    Working...
    X