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?

2 Answers 2

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

7
  • Ah, makes sense now. The whole comparison to C# properties is really confusing. At best they seem syntactically similar. Nov 16, 2012 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? Nov 16, 2012 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. Nov 16, 2012 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. Nov 18, 2012 at 23:20
  • 1
    @Brian, this isn't really how stack exchange works. What you should be doing is asking a new question. Nov 12, 2013 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;
        ...
    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .