7

The Force.com Apex Code Developer's Guide documentation for Apex Properties:Using Automatic Properties contains the following:

Properties do not require additional code in their get or set accessor code blocks. Instead, you can leave get and set accessor code blocks empty to define an automatic property. Automatic properties allow you to write more compact code that is easier to debug and maintain. They can be declared as read-only, read-write, or write-only. The following example creates three automatic properties:

public class AutomaticProperty {
   public integer MyReadOnlyProp { get; }
   public double MyReadWriteProp { get; set; }
   public string MyWriteOnlyProp { set; }
}

This had me scratching my head a bit. MyReadWriteProp makes perfect sense. The usefulness of MyReadOnlyProp and MyWriteOnlyProp eludes me.

Why would you want a read-only or write-only automatic property?

As far as I can tell you could never actually do anything useful with them. Either you could read a value that you could never set or write a value that you could never read.

Am I missing something or is this just a poor example in the documentation? Maybe there is some way to access the automatically generated backing property to set/read the value?

5

You are correct, this a badly written example, you can still set the values within the class. e.g

public class AutomaticProperty {
    public integer MyReadOnlyProp { get; private set; }
    public double MyReadWriteProp { get; set; }
    public string MyWriteOnlyProp { private get; set; }

    public AutomaticProperty() {
        this.MyReadOnlyProp = 25;
        ...
    }

}

This means at least in the controller context MyReadOnlyProp could be used as an outputText. There are plenty of other uses I won't go into unless you're interested

  • Ah, makes sense now. The whole comparison to C# properties is really confusing. At best they seem syntactically similar. – Daniel Ballinger Nov 16 '12 at 0:58
  • Syntactically speaking, one of the biggest gripes in the java world is the getter and setter pattern, and I think the creators of apex made a good decision with apex properties. I'm not that familiar with C#, how does it behave differently? – Daniel Blackhall Nov 16 '12 at 1:06
  • In C# MyWriteOnlyProp and MyReadOnlyProp would result in compile time errors (unless the class was abstract). "Automatically implemented properties must define both get and set accessors." You would need to create explicit member variables to use in the accessors. In your example I think the automatically generated backing member variable is being set in the constructor. – Daniel Ballinger Nov 16 '12 at 1:21
  • I can really see where the confusion lies :) You're right again, apex properties have an implicit backing variable with the same name as the property name. – Daniel Blackhall Nov 18 '12 at 23:20
  • 1
    @Brian, this isn't really how stack exchange works. What you should be doing is asking a new question. – Daniel Blackhall Nov 12 '13 at 3:29
2

Adding access modifiers to the getters and setters for MyReadOnlyProp and MyWriteOnlyProp allows them to be read/written to from within the class.

Expanding upon Daniel Blackhall's answer:

public class AutomaticProperty {
    public integer MyReadOnlyProp { public get; private set; }
    public double MyReadWriteProp { get; set; }
    public string MyWriteOnlyProp { private get; public set; }

    public AutomaticProperty() {
        this.MyReadOnlyProp = 25;
        ...
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.