John Mutuma
John Mutuma

Reputation: 3620

Apex: Setting Properties with the Indexing Notation

Is it possible to set a property on an instance of a class like so;

MyCls item = new MyCls();

item['propName'] = propValue;

Upvotes: 0

Views: 56

Answers (1)

David Reed
David Reed

Reputation: 2759

No. Apex does not support indexing notation on any object - custom classes, Map instances, or sObjects.

You can use the get() and put() methods on the Map and sObject built- classes to access fields by name or Map values. However, doing so on an sObject loses you compile-time field checking, and typically requires a lot of casting in statically-typed Apex. It's preferable to use standard access when possible.

This does not apply to custom classes unless you implement your own accessor methods.

Upvotes: 1

Related Questions