How to set a variable to NULL in ObjectScript?

In C #, I can do:

object foo = null; 

How to do this in a script object?

+6
source share
3 answers

For simple variables, there is no way to set a variable to undefined. Since the Cache Script object has free input, there is no need to set the link to the object to NULL, just change the value of the link to something else, most often an empty string, and the garbage collector will clear objects for which there is no active link.

For all practical purposes, just set this variable to the empty string "". If not, can you expand your question?

The properties of an object in a Script cache object are never resolved to undefined. If the value is undefined (because it has a SQL NULL value or a value has never been assigned), then the property will be resolved to an empty string value. If you want the property to contain an SQL null view, you can do SQL Insert or Update on the row corresponding to this object and set this field to NULL. If you set an empty string for an object property and save it, the SQL string for this object will not be NULL, but will have an empty string.

Basically, in the representation of objects there is no abstract representation of NULL. There are serialized values ​​for SQL NULL that allow NULL in the SQL view and an empty string in the Objects view.

By the way, the serialized NULL value in the SQL view is an empty string, and the serialized empty string is ASCII 0.

+10
source

To erase a variable from memory and garbage to collect a reference object, you can use the kill command

 Method Test() { set foo=##class(Obj).%New() // created object of class Obj. created variable foo pointing to this object. // do something set foo="" // Object of class Obj is now marked for garbage collection // but variable foo still exist // do something else kill foo // foo is now undefined // do something else } 

However, this is not necessary if you use ProcessBlock methods (by default in new versions of Cache) or new . In this case, all references to objects and variables will be destroyed automatically after the completion of your method

 Method Test() { set foo=##class(Obj).%New() // created object of class Obj. created variable foo pointing to this object. // do something } // after method finishes, foo is undefined and object of class Obj is destroyed 

If you just want to declare that a variable is of a specific type, you can use the #dim directive. It does nothing, it simply helps Studio to define a class of variables. It is sometimes useful if Studio cannot determine the class itself and you want to use the built-in helpers.

 Method Test() { #dim foo as Obj do ##class(Obj).GenerateSomething(.foo) write foo.Property // Studio will provide helper bar for foo properties and methods now } 
+6
source

The solution chosen is definitely inaccurate. If you want to set the variable to NULL, as indicated in your example, you must do as indicated by SSH. You would:

 K VARIABLE 

or

 KILL VARIABLE 
+2
source

All Articles