Re: Whither Object [message #34630] |
Wed, 09 April 2003 21:31  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
M. Katz (MKatz843@onebox.com) writes:
> What is the best way to define a new object variable such that:
> A) if the object class exists, then it is loaded.
> B) if the object class is unknown then we return obj_new() or set a
> failure flag somehow.
>
> Given an object class name in a string, I'd like to make a flexible
> definition:
>
> a = obj_new("MyClass")
>
> Then if there is a myclass__define.pro, we assign the object,
> otherwise we assign obj_new().
I'd do something like this:
objectClass = "MyClass
filename = StrLowCase(objectClass) + "__define.pro"
IF File_Which(filename, /Include_Current_Directory) EQ "" THEN $
thisObject = Obj_New() ELSE thisObject = Obj_New(objectClass)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: Whither Object [message #34768 is a reply to message #34630] |
Thu, 10 April 2003 10:23  |
MKatz843
Messages: 98 Registered: March 2002
|
Member |
|
|
Thanks, David.
Based on David's suggestion, I have this new funtion
function obj_new2, class
filename = StrLowCase(class) + "__define.pro"
return, (File_Which(filename, /Include_Current_Dir) EQ "") ? $
obj_new() : obj_new(class)
end
Note: there was a slight keyword error in David's post (heaven forfend!).
/Include_Current_Directory should be /Include_Current_Dir
and the former actually caused an error (IDL 5.6, Mac OS X).
My only concern is that this may not work in an IDL runtime distribution
which I am preparing. If everything is pre-compiled and nicely packaged,
then there won't be a directory to look through, true?
I'm thinking that we need a new IDL feature to handle this. Something
within OBJ_NEW() itself. Ok, who has connections on the inside...
Perhaps something more elegant than this
a = obj_new(class, /ReturnNullObjectIfNotFound, error=error)
M. Katz
|
|
|