Disabling randomization of sub classes to speed up randomization using rand_mode(0)

I have a class to be randomized which instantiates ~20 random classes. This is quite taxing on the constraint solver and actually for most tests we only require a subset of these to be randomized (different subset for each test) so what I’m trying to do is use an enable array (one bit per config) and then in pre_randomize() I look at the enable and set config.rand_mode(0) if the corresponding enable bit is 0.

Problem is setting rand_mode(0) on a class doesn’t seem to disable all constraints in the class and so I get constraint solver errors. What I’m trying to achieve is effectively not declaring the class as a rand class which I thought was what rand_mode(0) would do, is there any way to do this? We could randomize each enabled config individually one by one but then we would lose the ability to cross constrain across multiple enabled configs.
Any help much appreciated.

function void top_env_cfg::pre_randomize();
super.pre_randomize();

// Turn off randomization for any configs that are not enabled to speed up randomization
foreach(en_sub_cfg[cfg])
  if(en_sub_cfg[cfg] == 0)
    begin
        `uvm_info(get_name(), $sformatf("Disabling rand for cfg %0d", cfg), UVM_LOW)
        sub_cfg__base_cfg_h[cfg].rand_mode(0);
    end

endfunction: pre_randomize

In reply to BarryD:

Constraint_mode(0) has to be executed on objects and not on declaration. The code doesn’t show this. Could you please show some more code?

Found the problem but couldn’t reply sooner as my account needed to be verified for my first post.

The array sub_cfg__base_cfg_h is an array of base class handles to config objects which all extend from this base class. I thought because the objects were declared as rand then I could call rand_mode(0) on the base class handle and it would work as it is a pointer to the rand object. Reason for doing it this way is so I could iterate through the configs in a for loop.
Removing the for loop and calling rand_mode(0) on each of the extended objects instead has fixed the problem. Thanks.