Skip to main content

Altering Class Definitions

Altering Class Definitions

You can modify an existing class definition by opening a %Dictionary.ClassDefinitionOpens in a new tab object, making the desired changes, and saving it using the %Save() method.

You can create a new class by creating a new %Dictionary.ClassDefinitionOpens in a new tab object, filling in its properties and saving it. When you create %Dictionary.ClassDefinitionOpens in a new tab object, you must pass the name of the class via the %New() command. When you want to add a member to the class (such as a property or method), you must create the corresponding definition class (passing its %New() command a string containing "class_name.member_name") and add the object to the appropriate collection within the %Dictionary.ClassDefinitionOpens in a new tab object.

For example:

 Set cdef = ##class(%Dictionary.ClassDefinition).%New("MyApp.MyClass")
 If $SYSTEM.Status.IsError(cdef)  {
     Do $system.Status.DecomposeStatus(%objlasterror,.Err)
     Write !, Err(Err)
 Set cdef.Super = "%Persistent,%Populate"

 // add a Name property
 Set pdef = ##class(%Dictionary.PropertyDefinition).%New("MyClass:Name")
 If $SYSTEM.Status.IsError(pdef)  {
     Do $system.Status.DecomposeStatus(%objlasterror,.Err) 
     Write !,Err(Err)
 Do cdef.Properties.Insert(pdef)

 Set pdef.Type="%String"

 // save the class definition object
 Do cdef.%Save()
FeedbackOpens in a new tab