Whither Object [message #34635] |
Wed, 09 April 2003 15:48  |
MKatz843
Messages: 98 Registered: March 2002
|
Member |
|
|
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().
Perhaps I could create a new object-definition function with a success
flag, like this
a = obj_new2("SchoolOnSaturday", noclass=noclass)
;-- noclass=1 if it failed.
To date, I've been using something like this
name = "MyClass"
CATCH, Error_Status
if (Error_status NE 0) then begin
print, 'Error_status = ', Error_Status
print, 'Error message: ', !ERROR_STATE.MSG
print, 'No such class'
endif else begin
a = obj_new(name)
endelse
That seems to work a little, but CATCH catches everything and I don't
like using it. Another idea is that we could have a new function
called obj_test() where
YesItExists = obj_test("MyClass")
Does such a function exist already? Has anyone out there solved this
for themselves?
Thanks,
M. Katz
|
|
|
Re: Whither Object [message #34748 is a reply to message #34635] |
Fri, 11 April 2003 09:45  |
MKatz843
Messages: 98 Registered: March 2002
|
Member |
|
|
At the risk of monopolizing my own discussion, here's a function I
prepared that does both jobs. It first checks to see if the function
has already been compiled. That's good for the runtime distribution
where all objects must be precompiled. If that fails, it looks in the
directories for the __define.pro file. TRUE (1b) is returned if the
class exists. FALSE (0b) is returned if it fails.
function obj_test, class
if (obj_class(class) NE "") $ ;--- already compiled?
then return, 1b
;--- Search for it.
return, File_Which(StrLowCase(class) + "__define.pro" , $
/Include_Current_Dir) NE ""
end
|
|
|
Re: Whither Object [message #34758 is a reply to message #34635] |
Thu, 10 April 2003 15:25  |
MKatz843
Messages: 98 Registered: March 2002
|
Member |
|
|
The solution recommended by James Jay Jones at IDL's tech support
is to use obj_class().
I don't think I've ever used obj_class(). There's a first time for
everything. Here's the RTFM moment:
Syntax
Result = OBJ_CLASS( [Arg] [, COUNT=variable] [, /SUPERCLASS{must
specify Arg}] )
Return Value
Returns a string containing the name of the class or superclass. If
the supplied argument is not an object, a null string is returned. If
no argument is supplied, OBJ_CLASS returns an array containing the
names of all known object classes in the current IDL session.
Thanks David and James for your help.
M. Katz
|
|
|
Re: Whither Object [message #34767 is a reply to message #34635] |
Thu, 10 April 2003 10:39  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
M. Katz (MKatz843@onebox.com) writes:
> 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).
Aaauughhh! RSI is always doing this to me! I think
their agenda must include driving anal people to
distraction. :-(
One of the most grievous examples of this is the
GROUP keyword on XMANAGER. Uh, folks, that is *suppose*
to be a GROUP_LEADER!!!!
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
|
|
|