Is it possible to pass property to function parameter? Unfortunately I
got error 'Types of actual and formal var parameters must be
identical'.
type TStringProperty = procedure (const s:string) of object;
type
TMyClass = class
private
FID: integer;
procedure SetID(const Value: integer);
published
property ID: integer read FID write SetID;
end;
function C( var into_: TStringProperty):boolean;
begin
into_('a');
result:= true;
END;
procedure TEFT_Vserver.SetID(const Value: integer);
begin
FID := Value;
end;
Than You
In general it should. However note that properties can't be passed to VAR
parameters.
P.s. I tried to see what caused the problem, but your posted program is not
in compilable state.
var helped, but do I always should pass SetID property method. I
prefer to pass just property ID
type
TMyClass = class
private
FID: integer;
procedure SetID(const Value: integer);
published
property ID: integer read FID write SetID;
end;
function C( var into_: TStringProperty):boolean;
begin
into_('a');
result:= true;
END;
procedure TEFT_Vserver.SetID(const Value: integer);
begin
FID := Value;
C(ID);// Error 'Incompatible types: 'TIntegerProperty' and
'Integer''
C(SetID);// OK
end;
Ah now I understand. No, you can't obtain the setter name from the property
afaik by passing it.. The compiler treats them as semantically different,
hence the error.
> Hello,
>
> Is it possible to pass property to function parameter? Unfortunately I
> got error 'Types of actual and formal var parameters must be
> identical'.
Yes, but you can not pass properties as var parameter(*). But the error
you got is of a different kind: the types must really match. This is
also true for normal variables passed to a var parameter.
http://rvelthuis.de/articles/articles-pointers.html#refparams
<<
Actual parameters must be of the same type as the declared parameters,
i.e. you can not pass a TEdit if you declared the parameter as a
TObject. To avoid this, you can only use untyped reference parameters
instead
>>
(*) Properties are often function results. You can't pass those as var
parameters, so the general rule is that you can't pass properties as
var or out parameters.
FWIW, such tidbits are all described in the Delphi Language Guide,
which comes with every Delphi as part of the online help.
--
Rudy Velthuis http://rvelthuis.de
"The only one listening to both sides of an argument
is the neighbor in the next apartment" -- unknown
Too bad. You can't pass properties by reference. When you have a "var
string" parameter, the thing you pass must really be a string that can
be read from and written to.
> type
> TMyClass = class
> private
> FID: integer;
> procedure SetID(const Value: integer);
> published
> property ID: integer read FID write SetID;
> end;
>
>
> function C( var into_: TStringProperty):boolean;
> begin
> into_('a');
> result:= true;
> END;
That function does not require the parameter to be passed by reference.
Remove the "var" qualifier from the into_ parameter.
> procedure TEFT_Vserver.SetID(const Value: integer);
> begin
> FID := Value;
> C(ID);// Error 'Incompatible types: 'TIntegerProperty' and
> 'Integer''
The C function is expecting you to give it a pointer to a method that
takes a string as its first and only parameter. You're passing it
something else. I don't know exactly what you're passing since you
haven't shown the declaration of ID in the context of the TEFT_Vserver
class.
> C(SetID);// OK
Are you sure? You can't pass a method pointer by reference like that.
The call you show does not agree with the declaration you show.
> end;
--
Rob