# I want to use string as a parameter in parameterized class . I could not do it directly like we do for other datatypes. Can we use typecasting or is there any other method to do it?

**URL:** <https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Parameterized-classes\
**Created:** [May 3, 2018, 12:02pm UTC](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421 "2018-05-03T12:02:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyanka\_1996](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/df705f/32.png) [@Priyanka\_1996](https://verificationacademy.com/forums/u/Priyanka_1996)\
**Post date:** [May 3, 2018, 12:02pm UTC](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421/1 "2018-05-03T12:02:25Z")

</div>

The code is as follows:

```verilog
class sn_oops #(type A = int);
  A data;

   task set(int i);
     data = i;
     $display("The value of data is --> %0d",data);
   endtask: set

   function int get();
     return data;
   endfunction: get

 endclass: sn_oops

 class sn_oops_ch #(type B = int) extends sn_oops;
    B data;
    function super_keyword(int val);
      super.data = val;                                          
      $display("the value of data in parent class is --> %d",super.data);
      $display("the value of data in child class is --> %d",data);
    endfunction: super_keyword
  endclass: sn_oops_ch

//declaration of base class handles

   sn_oops #(bit[2:0]) sn_h1;                               
  // sn_oops #($cast(A, string)) sn_h2;--------------------->>>>>>> I TRIED CASTING HERE AS WELL BUT ERROR COMES
   sn_oops #(byte) sn_h2;
   sn_oops #(integer) sn_h3;

   sn_oops sn_h4;

  module main;
   initial begin

     //creation of class handles
     sn_h1 = new();
     sn_h2 = new();
     sn_h3 = new();
     sn_h4 = new();

// accessing class methods set/get;;;
      sn_h1.set(10); //out of bound values but no error occurs
      $display("the value of sn_h1 is %p",sn_h1);

      sn_h2.set(270); //out of bound values but no error occurs
      $display("the value of sn_h2 is %p",sn_h2);

      sn_h3.set($random);
      $display("the value of sn_h3 is %p",sn_h3);
      $display("--------------------------------------------------------");

      sn_h4.set(20);
      $display("accessing class methods using set and get methods\n");
      $display("Value of datamember 'data' = %d",sn_h4.get());
      $display("--------------------------------------------------------");

  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![sharvil111](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/sharvil111/32/3790_2.png) [@sharvil111](https://verificationacademy.com/forums/u/sharvil111)\
**Post date:** [May 3, 2018, 2:49pm UTC](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421/2 "2018-05-03T14:49:32Z")

</div>

_In reply to [Priyanka\_1996](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421):_

I am not sure about the intent of code. But instead of hard coding to int, we can use type A in the set/get function itself. The code works fine for me after the following changes.

```verilog
class sn_oops #(type A = int);
A data;

task set(A i); // Use type A
data = i;
$display("The value of data is --> %0d",data);
endtask: set

function A get(); // Use type A
return data;
endfunction: get

// ... 
sn_oops #(bit[2:0]) sn_h1; 
sn_oops #(string) sn_h2;
//...
sn_h2.set($sformatf("%0s","270")); // sformatf is just for illustration purpose.
//...

// Output:
The value of data is --> "270"
the value of sn_h2 is '{data:"270"}

```

Also, using “%0p” instead of “%0d” in the functions will be helpful.

A similar example is shown in [this thread](https://stackoverflow.com/questions/22696838/width-independent-functions).

---

<div class="post-metadata">

**Author:** ![Priyanka\_1996](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/df705f/32.png) [@Priyanka\_1996](https://verificationacademy.com/forums/u/Priyanka_1996)\
**Post date:** [May 4, 2018, 5:50am UTC](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421/3 "2018-05-04T05:50:55Z")

</div>

_In reply to [sharvil111](https://verificationacademy.com/forums/t/i-want-to-use-string-as-a-parameter-in-parameterized-class-i-could-not-do-it-directly-like-we-do-for-other-datatypes-can-we-use-typecasting-or-is-there-any-other-method-to-do-it/34421/2):_

I tried the above code and its working. Thank you.
