How to access class parameters based on string access?

Hello Folks,

Any idea/technique to access the class variables using string based access ? I tried below example to print the value of variables inside the class but instead it prints the string…


module print_loop;
 static int stat_cnt = 0; 
 string mux_ctrl_str;

class mux_ctrl_string;
    static int fruit0_slice0 = 10;
    static int fruit0_slice1 = 20;
    static int fruit0_slice2 = 30;
    static int fruit0_slice3 = 50;
endclass: mux_ctrl_string

mux_ctrl_string config_xtn;

initial begin
    $display("Value of Stat_cnt = %d\n", stat_cnt);
    $display("%h",stat_counter());    
    $display("Value of Stat_cnt = %d\n", stat_cnt);
    $display("%h",stat_counter());    
    $display("Value of Stat_cnt = %d\n", stat_cnt);
    $display("%h",stat_counter());    
    $display("Value of Stat_cnt = %d\n", stat_cnt);
    $display("%h",stat_counter());    
end


function bit [31:0] stat_counter();
  bit [31:0] stat_cnt_val_loc;
  for (int i=0; i<32; i=i+4) begin 
    if (stat_cnt==13) stat_cnt=0;
    stat_cnt_val_loc[i+:4] = stat_cnt;
    stat_cnt++;
  end 
  return (stat_cnt_val_loc);
endfunction: stat_counter


initial begin
for (int k=0; k<1; k++) begin
   for (int j=0; j<4; j++) begin
       $sformat(mux_ctrl_str, "config_xtn.fruit%0d_slice%0d", k ,j);
       $display(mux_ctrl_str); 
   end
 end
end

endmodule: print_loop


Output:

Value of Stat_cnt =           0

76543210
Value of Stat_cnt =           8

210cba98
Value of Stat_cnt =           3

a9876543
Value of Stat_cnt =          11

543210cb
config_xtn.fruit0_slice0
config_xtn.fruit0_slice1
config_xtn.fruit0_slice2
config_xtn.fruit0_slice3

Thought it will print the values 10,20,30,50 but instead it prints the string as a whole… How to solve such scenarios ?

In reply to desperadorocks:
You cannot use a string expression to reference a variable identifier. You should be thinking in terms of arrays, not individual variables.

In reply to dave_59:

@Dave : Thanks for the clarification. But if a class packet is having 100’s of such individual variable with name extensions, in that case what would be the best possible approach to access those variables ? apart from arrays …

In reply to desperadorocks:

The best possible approach would be to change the requirements that got you into this situation.

You can use this library, but it’s in the alpha stage, contains no documentation and no support. Also, it’s not tested on all simulators.