0
votes

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

MyCls item = new MyCls();

item['propName'] = propValue;
1

1 Answers

1
votes

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.