function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shaijan ThomasShaijan Thomas 

Class variable are not able to access dynamically

Hi All,

I am trying to access the class varible dynamically for one of my requirment. Please see the code.

public class CaseUpdateTestingClass {
    public string AssetSN = '1AED45';
    public string CDIDId='3498474';
    public string ContactFirstName='SalesForce';
}

CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string a = 'AssetSN'; // Here I do dynamic assignment
system.debug('*********** : '+es.get(a));

Thanks
Shaijan Thomas
AmrenderAmrender
Please use below code
 
​CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string a = es.AssetSN; // Here I do dynamic assignment
system.debug('*********** : '+a);

Regards
Amrender
Shaijan ThomasShaijan Thomas
Hi Amrender
Thanks for quick response

I update my scanario like this

​CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string ss = 'AssetSN;CDIDId;ContactFirstName'; // Here I do dynamic assignment
for ( string a : ss.split(';'))
system.debug('*********** : '+es.get(a));
AmrenderAmrender
Now your question is more clear.

AssestSN, CDIDId and ContactFirstName are the variables. Once we try to get the value, in this case it is treated as string and we will get exception. I dont think if it is possible in this way.

Regards
Amrender