Discussion (6)0
Log in or sign up to continue
Do ##class(%Dictionary.ClassDefinition).%DeleteId("Test.Class")
Set ClassDef=##class(%Dictionary.ClassDefinition).%New()
Set ClassDef.Name="Test.Class"
Set ClassDef.Super="%Persistent"
Set ClassDef.ProcedureBlock=1
Set MethDef=##class(%Dictionary.MethodDefinition).%New()
Set MethDef.Name="Name"
Set MethDef.ReturnType="%String"
; Uncomment for ClassMethod
; Set MethDef.ClassMethod=1
Set MethDef.FormalSpec="First:%Boolean,Second:%String=""Default"""
Do MethDef.Implementation.WriteLine($Char(9)_"If First Return 1")
Do MethDef.Implementation.WriteLine($Char(9)_"Return Second")
Set MethDef.parent=ClassDef
Do ClassDef.Methods.Insert(MethDef)
Do ClassDef.%Save()
#include %systemInclude
#include %occErrors
#include %occCPTJSgen
CreateClass() public {
  
  #dim ex As %Exception.AbstractException
  try {
    $$$AddAllRoleTemporaryInTry
    new $namespace
    
    s className="demo.test"
    
    d:##class(%Dictionary.ClassDefinition).%ExistsId(className##class(%Dictionary.ClassDefinition).%DeleteId(className)
    
    newClass=##class(%Dictionary.ClassDefinition).%New()
    newClass.Name=className
    newClass.ProcedureBlock=$$$YES
    s newClass.Super="%RegisteredObject"

    newProp=##class(%Dictionary.PropertyDefinition).%New()
    newProp.parent=newClass
    newProp.Name="p1"
    newProp.Type="%String"
    
    newMethod=##class(%Dictionary.MethodDefinition).%New()
    newMethod.parent=newClass
    newMethod.Name="Main"
    newMethod.ReturnType="%Status"
    newMethod.FormalSpec=$$$FormatText("a:%String=%1",$$$quote("Hello, World!"))
    newMethod.Implementation.WriteLine($$$TAB_"w a,!")
    newMethod.Implementation.WriteLine($$$TAB_"q $$$OK")
    
    $$$ThrowOnError(newClass.%Save())

    d $system.OBJ.Compile(className,"cu/multicompile=1")
     
    
  }catch(ex{
    "Error "ex.DisplayString(),!
  }
}

Result:

Class demo.test Extends %RegisteredObject ProcedureBlock ]
{

Property p1 As %String;

Method Main(As %String "Hello, World!"As %Status
{
 a,!
 q $$$OK
}

}