E2064 代入できない左辺値です (Delphi)
エラーと警告のメッセージ(Delphi) への移動
このエラー メッセージは、定数、定数パラメータ、関数の戻り値、読み取り専用プロパティ、読み取り専用プロパティのフィールドなどの、読み取り専用オブジェクトを変更しようとしたときに出力されます。
この問題を解決するには、2 つの方法があります。
- 代入が有効になるように、代入先の定義を変更する。
- 代入をすべて削除する。
例
program Produce; const c = 1; procedure p(const s: string); begin s := 'changed'; (*<-- Error message here*) end; function f: PChar; begin f := 'Hello'; (*This is fine - we are setting the return value*) end; begin c := 2; (*<-- Error message here*) f := 'h'; (*<-- Error message here*) end.
上の例では、定数パラメータ、定数、関数呼び出しの結果にそれぞれ代入しています。 これらはすべて不正です。 次の例で、これらの問題を解決する方法を示します。
program Solve; var c : Integer = 1; (*Use an initialized variable*) procedure p(var s: string); begin s := 'changed'; (*Use variable parameter*) end; function f: PChar; begin f := 'Hello'; (*This is fine - we are setting the return value*) end; begin c := 2; f^ := 'h'; (*This compiles, but will crash at run time*) end.
Delphi 2010 以降では、読み取り専用プロパティが公開するレコードのメンバに値を代入しようとしたときにも、コンパイラが E2064 を出力します。 次のレコード型を考えてみてください。
TCustomer = record Age: Integer; Name: String; end;
これは、クラスで読み取り専用プロパティに公開されます。
TExposing = class ... CurrentCustomer: TCustomer read SomeInternalField; ... end;
CurrentCustomer プロパティに値を代入しても、CurrentCustomer プロパティが公開するレコードのメンバに値を代入しても、コンパイラは E2064 エラーを出力します。