How to create a read-only property?

I use the TMS object inspector at runtime, but I assume that my question will be equally valid for Delphi during development.

I want to have a property that can be set programmatically (at run time) or hardcoded (at design time). It should be visible to the user, because the information is useful to him, and it should be changed at runtime by the program, but not by the user through the object inspector.

I tried

published property FileName : String read FFileName;

and the property is visible, but it can also change in the object inspector (and when reading excludes reading the exception zer0 addresses): - (

+5
source share
3 answers

published property FileName : String read FFileName;

, , , , :

public property RuntimeFilename: string read FFileName write FFilename;
//note that two properties, one published and one public point to the same field.

, Design-time
:

//Only writable during runtime.
private
  procedure SetFileName(Value: string);
published
  property FileName: string read FFileName write SetFileName;

....
procedure TMyClass.SetFileName(Value: string);
begin
  if csDesigning in Componentstate then {do nothing}
  else FFileName:= Value;
end;

, , ...


, write ....

, .
private, protected / public , , , .

, published , .

, / . write FFilename.

. init-code 1) , FFilename. SetFilename , .

1) (, .dfm dfm .exe)

+8

TMS, TMS.

+3

The easiest way:

private
  procedure SetFileName(Value: string);
published
  property FileName: string read FFileName write SetFileName;

....
procedure TMyClass.SetFileName(Value: string);
begin
  FFileName := FFileName;
end;
+2
source

All Articles