HallowMan07
HallowMan07

Reputation: 9

delphi property read function add value

TApplicationWrapper = class(TObjectWrapper)
private
  function GetMyFonk(): string;
  procedure SetMyFonk(myCmd: string);
published
  property myFonk: String read GetMyFonk write SetMyFonk;

...

function TApplicationWrapper.GetMyFonk(): string;
begin
  ShowMessage('GetMyFonk is Run');
  Result :='';
end;

procedure TApplicationWrapper.SetMyFonk(myCmd: string);
begin
  ShowMessage('SetMyFonk is Run');
end;

The program works this way. But I want to assign parameters to the GetMyFonk() function.

function GetMyFonk (myCommand : String ): string;

I get an error message.

[dcc32 Error] altPanellerU.pas(74): E2008 Incompatible types

My Screen Shot

How can I assign a value to the function?

Upvotes: 0

Views: 764

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598011

Your property simply does not support a getter function that takes parameters. For every parameter you want to add to the getter, you must add a corresponding parameter to the property and the setter, eg:

TApplicationWrapper = class(TObjectWrapper)
private
  function GetMyFonk(myCommand : String): string;
  procedure SetMyFonk(myCommand : String; Value : string);
published
  property myFonk[myCommand : String] : String read GetMyFonk write SetMyFonk;

...

function TApplicationWrapper.GetMyFonk(myCommand : String): string;
begin
  ShowMessage('GetMyFonk is Run w/ ' + myCommand);
  Result :='';
end;

procedure TApplicationWrapper.SetMyFonk(myCommand : String; Value: string);
begin
  ShowMessage('SetMyFonk is Run w/ ' + myCommand);
end;

And then you would have to access the property like this:

App: TApplicationWrapper;
... 
S := App.MyFonk['command'];
... 
App.MyFonk['command'] := S;

This is discussed in more detail in Embarcadero's documentation:

Properties (Delphi)

See the section on "Array Properties".

Upvotes: 5

Related Questions