# Can cast class type from another class? (Want to reduce redundant part)

**URL:** <https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, class, handle\
**Created:** [July 25, 2018, 1:57am UTC](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728 "2018-07-25T01:57:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lycos007](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/e95f7d/32.png) [@lycos007](https://verificationacademy.com/forums/u/lycos007)\
**Post date:** [July 25, 2018, 1:57am UTC](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/1 "2018-07-25T01:57:54Z")

</div>

I want to reduce redundant parts from my code.  
my code make a class handle(same handle name) from different class. and it is determined by its argument.

## for example,

```verilog
`ifdef useClassA
 classA handle;
 
 common part;
`endif

`ifdef useClassB
 classB handle;
 
 common part;
`endif

```

* * *

.  
.  
…

## So, I changed like this…

```verilog
case(usedClass) begin
 classA : classA handle;
 classB : classB handle;
...
end

common part;

```

* * *

Problem is,… the handle disappear outside of case statement.  
Is there any solution? (make handle from different class by its argument, but I don’t want the handle disappeared)  
thanks for your help.

---

<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:** [July 25, 2018, 2:33am UTC](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/2 "2018-07-25T02:33:25Z")

</div>

_In reply to [lycos007](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728):_

You need to explain how classA and classB are different from each other, and how you expect to use handle. Normally you would just extend classA and classB from a common base class. The you can selectively choose which class to construct, and store its handle in a base class variable.

---

<div class="post-metadata">

**Author:** ![lycos007](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/e95f7d/32.png) [@lycos007](https://verificationacademy.com/forums/u/lycos007)\
**Post date:** [July 25, 2018, 3:08am UTC](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/3 "2018-07-25T03:08:50Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/2):_

Yes, classA and classB both inherited from uvm\_reg.  
and in common part, I use methods of classA and classB.  
Actually, I can’t figure out how can I use methods. Even though I don’t use new or create to make object. (I didn’t make this code…)

So, relation between classA and classB is not parents and child. In this case, Can I cuse $cast?

+) I consider another solution… That is using fuction. But, in this case, function argument are different because handle class types are different… Is there any way to use different(or may be ambiguous) argument type in fuction?

case(usedClass) begin  
classA : begin  
classA handle;  
func\_comm(handle);  
end  
classB : begin  
classB handle;  
func\_comm(handle);  
end  
…  
end

funcion func\_comm()  
common\_part;  
endfunction

Thanks for your help!!

---

<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:** [July 25, 2018, 1:01pm UTC](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/4 "2018-07-25T13:01:34Z")

</div>

_In reply to [lycos007](https://verificationacademy.com/forums/t/can-cast-class-type-from-another-class-want-to-reduce-redundant-part/34728/3):_

Difficult to help you without knowing the ultimate goal of what you are trying to accomplish.
