Reputation: 3620
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
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