Question

In c# I can do:

object foo = null;

How do I do this in object script?

Was it helpful?

Solution

For simple variables, there isn't a way to set a variable to have an undefined value. Since Cache Object Script has loose typing, it isn't necessary to set an object reference to NULL, it is enough to change the value of the reference to anything else, most commonly empty string, and the garbage collector will clean up the objects for which there is no active reference.

For all practical purposes, setting that variable to empty string "", is enough. If not, can you expand on your question?

Object properties in Cache Object Script never resolve to an undefined value. If the value is undefined (because it has the SQL value of NULL or has never been assigned a value), then the property will resolve to the value of empty string. If you want the property to contain the SQL representation of null, you can do an SQL Insert or Update on the row that corresponds to that object and set the field to NULL. If you set the object's property to empty string and save it, the SQL row for that object will not have NULL, but will have empty string.

Basically, there isn't really an abstract representation of NULL in the object view. There are serialized values for SQL NULL that resolve to NULL in the SQL view and empty string in the Objects view.

Incidentally, the serialized value of NULL in the SQL view is empty string, and the serialized value of empty string is ASCII 0.

OTHER TIPS

To erase variable from memory and garbage collect referenced object, you can use 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 ProcedureBlock methods (it's default in new Cache versions) or new command. In this case, all object references and variables will be destroyed automatically after your method finishes

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 the variable is of certain type, you may use #dim directive. It does nothing, just helps Studio to determine variable class. Sometimes it's useful if Studio can't determine class itself and you want to use its inline 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
}

The selected solution is definitely inaccurate. If you wanted to set a variable to NULL as stated in your example, you would do as stated by SSH. You would:

K VARIABLE

or

KILL VARIABLE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top