RE: Question for the Java Gurus
RE: Question for the Java Gurus
- Subject: RE: Question for the Java Gurus
- From: <email@hidden>
- Date: Thu, 22 Apr 2004 09:11:08 +0200
- Thread-topic: Question for the Java Gurus
Hi!
I am not sure what you are trying to do. There is really no point in casting to assign varaible of type Object.
The only "good" it could to is to check the object's type. A ClassCastException is thrown if it does not match your expectations. Using exceptions to control normal execution flow is a big no-no though. You should rather look for a condition to test on and branch accordingly. In this situation an 'instanceof' might be appropriate.
In your specific example, the class to test against is not known at compile time. It is specified by an argument to your method. You'll thus need to write a few extra lines of code:
public getOtherObjects(String entity, NSArray objects) {
Class entityClass = Class.forName(entity); // Will need to handle ClassNotFoundException, ... as appropriate
for(int i=0;i<objects.count();i++) {
Object tempOb = array.objectAtIndex(0);
if (entityClass.isInstance(tempOB)
{
// Do your stuff
}
else
{
// Cry out loud
}
}
}
Pierre
-----Original Message-----
From: email@hidden
[mailto:email@hidden]On Behalf Of Owen McKerrow
Sent: Thursday, April 22, 2004 5:49 AM
To: Apple Webobjects List List
Subject: Question for the Java Gurus
Hi All,
How can I make the following code work if entity is an instance of
String passed in as an argument to the function. i.e. Can I cast to a
specific Class if all I have is the name of the Class as a String ? If
not is there another way I can do this ?
public getOtherObjects(String entity, NSArray objects) {
for(int i=0;i<objects.count();i++) {
Object tempOb = (entity)array.objectAtIndex(0);
}
}
Thanks
Owen McKerrow
"As of tomorrow, employees will only be able to access the building
using individual security cards. Pictures will be taken next Wednesday
employees will receive their cards in two weeks."
- "Dilbert Quotes" Winner, Fred Dales, Microsoft Corp
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
**********************************************************************
This email and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender
of this message. (email@hidden)
This email message has been checked for the presence of computer
viruses; however this protection does not ensure this message is
virus free.
Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
**********************************************************************
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.