# How to constraint a variable in extended class based on a variable in base class?

**URL:** <https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-constraint, Extended-Class\
**Created:** [March 7, 2021, 1:09am UTC](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794 "2021-03-07T01:09:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yasaswini](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/45deac/32.png) [@Yasaswini](https://verificationacademy.com/forums/u/Yasaswini)\
**Post date:** [March 7, 2021, 1:09am UTC](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/1 "2021-03-07T01:09:53Z")

</div>

Below is an example I was trying. Even though the kind variable is being randomized to both II and IEEE, I see that in the extended class constraint, it only prints kind as II and thus keeps vlan variable as zero. Am i missing something here?

```verilog
class macframe;
    typedef enum {II,IEEE} kind_e;
    randc kind_e kind;
    
    function display();
      $display("the kind is %p",kind);
    endfunction
  endclass
  
  class bvlan extends macframe;
    bit vlan=0;
    constraint myconstraint{
      if(super.kind == "II") vlan == 1;}
      
      function display();
        super.display();
        $write("%d",vlan);
      endfunction             
  endclass

module tb;
macframe mac;
bvlan vlanobj;  
  initial begin
    mac = new();
    vlanobj = new();
    repeat(10)
      begin
         mac.randomize();
         mac.display();
         vlanobj.display();
      end
   end
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 7, 2021, 4:32am UTC](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/2 "2021-03-07T04:32:12Z")

</div>

_In reply to [yr](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794):_

Once again, please use [code tags](https://verificationacademy.com/forums/announcements/please-format-your-code-code-and/code-tags) making your code easier to read. I have added them for you.

You have created two distinct objects and only randomized one of them. When you call vlanobj.display(), vlan still has its initial value.

Have you looked at [this course](https://verificationacademy.com/courses/systemverilog-oop-for-uvm-verification) yet?

---

<div class="post-metadata">

**Author:** ![Yasaswini](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/45deac/32.png) [@Yasaswini](https://verificationacademy.com/forums/u/Yasaswini)\
**Post date:** [March 11, 2021, 12:09am UTC](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/3 "2021-03-11T00:09:59Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/2):_

Hi Dave , sorry about the tags. I will add them from next time.

I understand that i have randomized only one object. So when does the randomized value of kind be used to constrain the value of vlan? because even if i do the following , the initialized value of vlan which is zero is printing.

```verilog
class macframe;
    typedef enum {II,IEEE} kind_e;
    randc kind_e kind;
 
    function display();
      $display("the kind is %p",kind);
    endfunction
  endclass
 
  class bvlan extends macframe;
    bit vlan=0;
    constraint myconstraint{
      if(super.kind == "II") vlan == 1;}
 
      function display();
        super.display();
        $write("%d",vlan);
      endfunction             
  endclass
 
module tb;
macframe mac;
bvlan vlanobj;  
  initial begin
    mac = new();
    vlanobj = new();
    repeat(10)
      begin
         mac.randomize();
        vlanobj.randomize();
         mac.display();
         vlanobj.display();
      end
   end
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 11, 2021, 12:38am UTC](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/4 "2021-03-11T00:38:19Z")

</div>

_In reply to [yr](https://verificationacademy.com/forums/t/how-to-constraint-a-variable-in-extended-class-based-on-a-variable-in-base-class/38794/3):_

Because you did not declare it as a random variable

```verilog
rand bit vlan=0;

```
