Jump to content
PatV

using an array of with TOmniValue

Recommended Posts

Delphi 10.3

Omnithead 3.07.5

 

Hi All

 

here is a definition of an array of ..

 

  TpDataType  = (dtNone, dtInteger, dtDateTime, dtString, dtBlob);

 

  rParameter = record
    Field   : string;
    Value   : Variant;
    AsValue : TpDataType;
  end;

 

  TParameters = array of rParameter;

 

I would like to pass it to a worker as parameter, so 

 

    CreateTask(Worker)
      .MonitorWith(OTLMonitor)
      .SetParameter('Params',TOmniValue.Create([Params]);   

 

I've tried also

      .SetParameter('Params',TOmniValue.FromArray<TParameters>(Params))

 

and convert it again like 

 

function TConnectionPoolData.WithParameters(const aValue : TOmniValue)  : IConnectionPoolData;

begin
  FParams :=  aValue.CastTo<TParameters>;

  Result:=Self;

end;
 

But it's not working as expected, as I'm getting bunch of data   

 

Do I need to use a class instead ?

 

Thanks 

 

Patrick

 


 

 

 

 

Edited by PatV
add omnithread version

Share this post


Link to post

This works:

 

program Project191;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  OtlCommon;

type
  TpDataType  = (dtNone, dtInteger, dtDateTime, dtString, dtBlob);

  rParameter= record
    Field   : string;
    Value   : Variant;
    AsValue : TpDataType;
  end;

  TParameters = TArray<rParameter>;

var
  params, params2: TParameters;
  ov: TOmniValue;

begin
  SetLength(params, 2);

  params[0].Field := 'a'; params[0].Value := 1; params[0].AsValue := dtInteger;
  params[1].Field := 'b'; params[1].Value := Now; params[1].AsValue := dtDateTime;

  ov := TOmniValue.FromArray<rParameter>(params);

  params2 := ov.ToArray<rParameter>;
end.

You will have to convert 'array of rParameter' to 'TArray<rParameter>'. TOmniValue has special support (FromArray, ToArray) for the latter but not for the former.

Share this post


Link to post

Thanks a lot Primož, it helps me a lot.

 

And thanks for sharing Omnithread

 

Patrick

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×