Invalid cast type error

G

Glenn Robinson

Joona said:
Glenn Robinson <[email protected]> scribbled the following
Michael Borgwardt wrote:
Glenn Robinson wrote:
I create a new class object at runtime using:

Class dmClass = Class.forName(dmClassName);
[]
How can I use dmClass as the cast type in a statement?

You cannot, that's impossible.

What are you trying to achieve with it anyway?
I have:
interface DataManager
class SasDMImp which implements DataManager
I do the following:
String dmClassName = "SasDMImp";
Class dmClass = Class.forName(dmClassName);
I now want to use the SasDMImp class to cast a new object and this is the
part I'm having trouble with.
If I hard code the class name I use:
SasDMImp dm = (SasDMImp) Naming.lookup("rmi://" +host + registry);
I want to replace the hard coded SasDMImp with dmClass but I get the
Invalid Cast type when I do this.
Hope this helps.

This is a well-known situation. Our application uses this form of code
about a hundred times.
My answer to your problem is: Forget about casting to SasDMImp. Cast to
DataManager instead, as it's a known interface, and code that uses your
data manager shouldn't have to care about whether you have a SadDMImp or
some other implementation. If it does, you have a design problem.

Thanks, this is what I wanted. I cast using the interface DataManager and
all worked just fine.

Appreciate you comments and help.

Glenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top