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?

The code is as follows:

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

In reply to Priyanka_1996:

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.

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.

In reply to sharvil111:

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