6

C# では、次のことができます。

object foo = null;

オブジェクトスクリプトでこれを行うにはどうすればよいですか?

4

3 に答える 3

10

単純な変数の場合、変数に未定義の値を設定する方法はありません。キャッシュ オブジェクト スクリプトは型指定が緩いため、オブジェクト参照を NULL に設定する必要はありません。参照の値を他のもの (最も一般的には空の文字列) に変更するだけで十分です。アクティブな参照はありません。

実際には、その変数を空の文字列 "" に設定するだけで十分です。そうでない場合は、質問を拡大できますか?

Cache Object Script のオブジェクト プロパティが未定義の値に解決されることはありません。値が未定義の場合 (SQL 値が NULL であるか、値が割り当てられていないため)、プロパティは空の文字列の値に解決されます。プロパティに null の SQL 表現を含める場合は、そのオブジェクトに対応する行で SQL 挿入または更新を実行し、フィールドを NULL に設定できます。オブジェクトのプロパティを空の文字列に設定して保存すると、そのオブジェクトの SQL 行は NULL ではなく、空の文字列になります。

基本的に、オブジェクト ビューには NULL の抽象的な表現はありません。SQL ビューでは NULL に解決され、オブジェクト ビューでは空の文字列に解決される SQL NULL のシリアル化された値があります。

なお、SQL ビューでの NULL のシリアライズ値は空文字列、空文字列のシリアライズ値は ASCII 0 です。

于 2012-10-24T19:38:55.717 に答える
6

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
}
于 2012-10-25T04:14:18.993 に答える
2

選択したソリューションは間違いなく不正確です。例で述べたように変数を NULL に設定したい場合は、SSH で述べたように行います。するでしょう:

K VARIABLE

また

KILL VARIABLE
于 2013-08-21T19:51:20.213 に答える