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

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,

`ifdef useClassA
 classA  handle;
 
 common part;
`endif

`ifdef useClassB
 classB handle;
 
 common part;
`endif


.
.

So, I changed like this…

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.

In reply to lycos007:

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.

In reply to dave_59:

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!!

In reply to lycos007:

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