#1 2023-07-26 03:02:26

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 56

Doubts about Variant with latebinding.

I need make a class that expose a published property called Params.
I would like that this property as Variant with latebinding.
How I can do this?

  v := TMyClasse.Create;
  with Variant(v.Params^) do  // this works, but i dislike have to use pointer
  begin
    Codigo := 10;
    Nome := 'Teste de acéntúàção';
    Data := StrToDate('18/09/1976');
    Valor := 1254.96;
  end;
  // v.Params = {"Codigo":10,"Nome":"Teste de acéntúàção ","Data":"1976-09-18","Valor":1254.96}
  
  with v.Params do  // <<<< how make this way (using Params: Variant)???????????
  begin
    Codigo := 10;.Codigo := 10;
    Nome := 'Teste de acéntúàção';
    Data := StrToDate('18/09/1976');
    Valor := 1254.96;
  end;
  // v.Params = {}

how should be my class?????
In this moment it use pointer. how return variant latebinding? or other thing to can use latebinding????

  TMyClasse = class
  private
    FParams: Variant;
    function GetParams: Pointer;
  public
    constructor Create;
    property Params: Pointer read GetParams;
  end;
...
constructor TMyClasse.Create;
begin
  inherited;
  TDocVariant.New(FParams, [dvoValueCopiedByReference]);
end;

function TMyClasse.GetParams: Pointer;
begin
  Result := @FParams;
end;

Offline

#2 2023-07-26 06:54:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: Doubts about Variant with latebinding.

To be honest, I don't expect "with ... do begin ... end" to work with late binding.
It is very confusing for the compiler and human reader.
Whatever solution you would find (like a pointer) just adds confusion.

I won't use late binding for such code, but rather TDocVariantData.Update([]) by name/value pairs.
Perhaps with _Safe().

Offline

#3 2023-07-27 12:56:45

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 56

Re: Doubts about Variant with latebinding.

ab wrote:

To be honest, I don't expect "with ... do begin ... end" to work with late binding.
It is very confusing for the compiler and human reader.
Whatever solution you would find (like a pointer) just adds confusion.

I won't use late binding for such code, but rather TDocVariantData.Update([]) by name/value pairs.
Perhaps with _Safe().

You sugestion is something like this?

  TMyClasse = class
  private
    FParams: TDocVariantData;
  public
    procedure Params(const pName: String; pValue: Variant);
...
constructor TMyClasse.Create;
begin
  inherited;
  FParams.Init;  <<<<<<< _Safe(Need Variant) and return PDocVariant, then I have used .Init. Some problem with this?
end;

procedure TMyClasse.Params(const pName: String; pValue: Variant);
begin
  FParams.AddOrUpdateValue(pName, pValue);  <<<<<<<<
end;

There are some way to use a property published to use latebiding? If yes, what type should be this property? else thanks AB! I will following this way.

Offline

#4 2023-07-27 15:38:21

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: Doubts about Variant with latebinding.

mrbar2000 wrote:

There are some way to use a property published to use latebiding?

You may find inspiration in this article with sample source code.

With best regards
Thomas

Offline

Board footer

Powered by FluxBB