Dynamic class creation

83 views
Skip to first unread message

wesselch

unread,
Feb 16, 2010, 2:58:46 AM2/16/10
to InterSystems: Zen Community
Hello,

I have following code:

I quer {
S class="de.hhla.frucht.email.gui.anhangquer.cls"
S ref=##class("de.hhla.frucht.email.gui.anhangquer").%New()
}
ELSE {
S class="de.hhla.frucht.email.gui.anhang.cls"
S ref=##class("de.hhla.frucht.email.gui.anhang").%New()
}

Is there any chance to use the scalar 'class' as parameter for the
class constructor, e.g.
S ref=##class(class).%New() ; that doesn't work

christian

Michael Braam

unread,
Feb 16, 2010, 3:09:33 AM2/16/10
to intersys...@googlegroups.com
You could use the $zobjclassmethod function:

...
Set ref = $zobjclassmethod(class,"%New")
...

should work.

See the docs for more information.

Hope this helps.


Michael Braam
Senior Sales Engineer

InterSystems GmbH
Hilpertstr. 20a
D-64295 Darmstadt
Tel.: +49 (0)6151 / 1747-0
Fax: +49 (0)6151 / 1747-11
Michae...@InterSystems.com
www.InterSystems.de
Geschäftsführer: Jörg Klingler, John Macheras, Stephen G. Baron
Eingetragen beim AG Darmstadt unter HRB 4966


-----Ursprüngliche Nachricht-----
Von: intersys...@googlegroups.com
[mailto:intersys...@googlegroups.com] Im Auftrag von wesselch
Gesendet: Dienstag, 16. Februar 2010 08:59
An: InterSystems: Zen Community
Betreff: [InterSystems-Zen] Dynamic class creation

Hello,

I have following code:

christian

--
You received this message because you are subscribed to the Google Groups
"InterSystems: Zen Community" group.
To post to this group, send email to InterSys...@googlegroups.com
To unsubscribe from this group, send email to
InterSystems-Z...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/InterSystems-ZEN?hl=en
Zen Community Terms and Conditions:
http://groups.google.com/group/InterSystems-ZEN/web/community-terms-and-cond
itions


Michael Braam

unread,
Feb 16, 2010, 3:12:49 AM2/16/10
to intersys...@googlegroups.com
Or in a more object style:

Set ref = $system.OBJ.New(class)

regards



Michael Braam
Senior Sales Engineer

InterSystems GmbH
Hilpertstr. 20a
D-64295 Darmstadt
Tel.: +49 (0)6151 / 1747-0
Fax: +49 (0)6151 / 1747-11
Michae...@InterSystems.com
www.InterSystems.de
Geschäftsführer: Jörg Klingler, John Macheras, Stephen G. Baron
Eingetragen beim AG Darmstadt unter HRB 4966


-----Ursprüngliche Nachricht-----
Von: intersys...@googlegroups.com
[mailto:intersys...@googlegroups.com] Im Auftrag von wesselch
Gesendet: Dienstag, 16. Februar 2010 08:59
An: InterSystems: Zen Community
Betreff: [InterSystems-Zen] Dynamic class creation

Hello,

I have following code:

christian

--

Daniel Gregory

unread,
Feb 16, 2010, 3:17:16 AM2/16/10
to intersys...@googlegroups.com
Hello Christian,
 
There are some utilities that will help you achieve whgat you are trying to do.  There is a method in $System.OBJ called "New".   Here is the syntax:
Set obj = $System.OBJ.New("Person")
 
You can also use the $ZOBJCLASSMETHOD utility.  This allows you to pass parameters into the constructor...here is an example class that I will create an  instance of using the utility...the constructor has two parameters...
///Example for Christian
Class spirit.test.cls.Sample Extends %RegisteredObject
{

Property First As %String;

Property Last As %String;

/// This callback method is invoked by the <METHOD>%New</METHOD> method to
/// provide notification that a new instance of an object is being created.
///
/// <P>If this method returns an error then the object will not be created.
/// <p>It is passed the arguments provided in the %New call.
/// When customizing this method, override the arguments with whatever variables and types you expect to receive from %New().
/// For example, if you're going to call %New, passing 2 arguments, %OnNew's signature could be:
/// <p>Method %OnNew(dob as %Date = "", name as %Name = "") as %Status
Method %OnNew(first as %String,last as %String) As %Status [ Private, ProcedureBlock = 1, ServerOnly = 1 ]
{
set ..First=first
set ..Last=last
Quit $$$OK
}

}
Here is how I would use the utility to create an instance...
 
USER>s cls="spirit.test.cls.Sample"
 
USER>s method="%New"
 
USER>s p1="Danny"
 
USER>s p2="Gregory"
  
USER>s object=$ZOBJCLASSMETHOD(cls,method,p1,p2)
 
USER>w object.First
Danny
 
USER>w object.Last
Gregory
There are two methods for creating objects.  Please also take a peek at the $ZOBJMETHOD utility...it allows you to dynamically call an instance method...versus the $ZOBJCLASSMETHOD, which allows you to call static methods on a class.  Also take a peek at $ZOBJPROPERTY.
 
I hope that helps.
 
Sincerely,
Danny
 


From: wesselch <wes...@hhla.de>
To: InterSystems: Zen Community <intersys...@googlegroups.com>
Sent: Tue, February 16, 2010 2:58:46 AM
Subject: [InterSystems-Zen] Dynamic class creation
Reply all
Reply to author
Forward
0 new messages