function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mario HammerMario Hammer 

Getters and Setters for Observables

Hi everyone,

I have this piece of code:
/* Free Allotments */
    public ObservableInteger freeAllotments = new ObservableInteger();
    public Integer getFreeAllotments(){ return freeAllotments.getInteger(); }
    public void setFreeAllotments(Integer newValue) { freeAllotments.setInteger(newValue); }
The ObservableInteger is a class that follows the standard observer pattern. I want this to look like an Integer property for the visualforce page to use standard components:
<apex:inputText value="{!freeAllotments}" />
When I try to save the VF page, I get: Read only property 'ContractWizardCtrl.freeAllotments'

Does anyone know how to achieve this?
Mario HammerMario Hammer
Mario Hammer
I've found one dirty hack. Did some renaming for the Observable and inserted an Integer that fits to the getters / setters:
public Integer freeAllotments; 
public ObservableInteger freeAllotmentsO = new ObservableInteger(); 
public Integer getFreeAllotments(){ return freeAllotmentsO.getInteger(); } 
public void setFreeAllotments(Integer newValue) { freeAllotmentsO.setInteger(newValue); }
I don't like this solution but the behaviour of the page is as I want it. Does anyone have a better solution?
James LoghryJames Loghry

Mario, I think the way you have it is about the cleanest you're going to get it.  Alternatively, you could combine the get and set methods in one varaible declaration, as seen in the example below.  In the example, I've also moved the ObservableInteger initialization to the constructor, as in my opinion, this is cleaner yet.

private ObservableInteger freeAllotmentsO {get; set;}

public Integer freeAllotments {
    get{
        freeAllotmentsO.getInteger();
    }
    set{
        freeAllotmentsO.setInteger(value);
    }
}

//Constructor
public MyClass(){
    freeAllotmentsO = new ObservableInteger();
}
Mario HammerMario Hammer
James, that solution looks quite good. I'll use it. Thank you!