Can we use enum variable in hierarchical class

class configuration;
int numoftxns;
enum int{b_wr,b_rd,wr_rd}CMD_TYPE; // burst write, burst read, write followed with read
enum int{d_random,d_incr,d_decr,d_userpat,d_constant}DATA_TYPE;
enum int{a_random,a_incr,a_decr,a_userpat,a_constant}ADDR_TYPE;
endclass

class testcase;

configuration cfg;

function new(configuration cfg);

this.cfg=cfg;
endfunction

task run();
cfg.numoftxns=20;
cfg.CMD_TYPE=8;
cfg.DATA_TYPE=10;
cfg.ADDR_TYPE=10;
endtask

endclass

after compilation I got this error

n enum variable ‘this.cfg.CMD_TYPE’ may only be assigned the same enum typed variable or one of its values. Value ‘8’ requires an explicit cast.
An enum variable ‘this.cfg.DATA_TYPE’ may only be assigned the same enum typed variable or one of its values. Value ‘10’ requires an explicit cast.

An enum variable ‘this.cfg.ADDR_TYPE’ may only be assigned the same enum typed variable or one of its values. Value ‘10’ requires an explicit cast.

In reply to suresh M:

It will not understand what is 8, try use $cast (cfg.CMD_TYPE, 8);

By default your enum has values:


enum int{b_wr = 0, b_rd = 1, wr_rd = 2}CMD_TYPE; // burst write, burst read, write followed with
...

8 - unknown state

You can set another initial value for this enum to allow use of 8:


enum int{b_wr = 8, b_rd, wr_rd} CMD_TYPE; // burst write, burst read, write followed with
...

In reply to suresh M:

There are several problems with your code, and the previous two replies are incorrect.

Why do you define a set of enum variables and proceed to assign them with values that are not one of the enumerated labels? This is illegal even if the integral value matches the encoded label value. But SystemVerilog allows you to use an explicit cast to allow the variable to hold an illegal value. However, since you used an anonymous enum type to define your variables, there is no type name to use with the cast.

It is always a good idea to use a typedef with an enum. Then you can create multiple variables with the same type to pass around. An anonymous enum can only be used for one variable.

So to fix your code, you need to either change the RHS of the enum variable assignments from numeric values to one of the declared enum label names, or you need to define a typedef for the enum and use an explicit cast.