#1 2024-05-11 21:18:44

nakisen
Member
Registered: 2021-04-30
Posts: 16

TDocVariantData Range check error

Hi

I'm getting an error when working with TDocVariantData.

// This code works
procedure TForm1.Button3Click(Sender: TObject);
var
  LObj:TDocVariantData;
  LKey, LValue:Utf8String;
begin
  LKey:='id';
  LValue:='1001-test;@6f2dcdcc4303a085a18d98419e1fb374';
  LObj.AddValue(LKey, LValue);

  LKey:='id2';
  LValue:='50';
  LObj.AddValue(LKey, LValue);

  memo1.Lines.Add(LObj.ToJson);
end;
// This code is not working
procedure TForm1.Button2Click(Sender: TObject);
var
  LObj:TDocVariantData;
begin
  FillParams(PUtf8Char(FParamStr), LObj);
  memo1.Lines.Add(LObj.ToJson);
end;

procedure FillParams(AParams:PUtf8Char; var AObj:TDocVariantData);
var
  LKey, LValue:Utf8String;
begin

  if (AParams = nil) then exit;

  repeat
    AParams := UrlDecodeNextNameValue(AParams, LKey, LValue);
    // Lkey='id', LValue='1001-test;@6f2dcdcc4303a085a18d98419e1fb374'
    AObj.AddValue(LKey, LValue);  <--- Range check error

    if AParams = nil then  break;
  until AParams^ = #0;

end;

Why am I getting this error? Am I missing something?

Last edited by nakisen (2024-05-11 21:22:45)

Offline

#2 2024-05-11 21:54:53

nakisen
Member
Registered: 2021-04-30
Posts: 16

Re: TDocVariantData Range check error

Interestingly, even though I haven't made any changes in the code, sometimes I get the "Range check error" error and sometimes the "Add: Unexpected [id] object property in an array" error.

Compiler/IDE: Delphi 12.

Offline

#3 2024-05-12 06:16:03

nakisen
Member
Registered: 2021-04-30
Posts: 16

Re: TDocVariantData Range check error

I did initialization and everything is fine roll smile

  LObj.Init;

Offline

#4 2024-05-12 13:53:26

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

Re: TDocVariantData Range check error

TDocVariantData needs initialization.

If you are lost, try to use IDocList/IDocObject which are much natural to work with.

Online

#5 2024-05-13 09:19:45

nakisen
Member
Registered: 2021-04-30
Posts: 16

Re: TDocVariantData Range check error

I started using "IDocList/IDocObject". How can I do custom filtering using TVariantCompare? Are there any examples? I need to make a comparison like "startsWith/endsWith" or "%like%".

Offline

#6 2024-05-13 09:36:12

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

Re: TDocVariantData Range check error

TVariantCompare is to compare... variants... so to compare values, e.g. in a IDocList.
Is it what you cant to do?

Then you need to work with a custom comparison function.

function MyCompare(const V1, V2: variant): PtrInt;
var u1, u2: RawUtf8;
begin
  VariantToUtf8(V1, u1);
  VariantToUtf8(V2, u2);
  if .... then   // put the comparison between u1 and u2 here
    result := 0 // match (=coEqualTo)
  else
   result := 1; // not match
end;

Then use this compare=MyCompare and match=coEqualTo for the IDocList.Filter() method parameters.

As an alternative, you can use the IDocList.Objeccts() iterator in a "for" loop, then search manually into each iterated IDocDict for a match.

Online

#7 2024-05-13 11:37:54

nakisen
Member
Registered: 2021-04-30
Posts: 16

Re: TDocVariantData Range check error

I want to do something like below. I did what I wanted manually with the loop. Thanks.

IDocList.Objects('name LIKE test%')
or
IDocList.Objects('name', '%test%', coLike)
or
IDocList.Objects('name', 'test', coInclude)
or
IDocList.Objects('name', 'test', coStartsWith)
or
IDocList.Objects('name', 'test', coEndsWith)

Last edited by nakisen (2024-05-13 11:42:33)

Offline

#8 2024-05-14 16:33:43

nakisen
Member
Registered: 2021-04-30
Posts: 16

Re: TDocVariantData Range check error

Hi

Is this the correct method to delete the object/objects?

  for AObj in AList.Objects(LCompareStr) do
    AList.Remove(AObj.AsVariant);

Last edited by nakisen (2024-05-14 16:34:07)

Offline

#9 2024-05-14 17:21:24

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

Re: TDocVariantData Range check error

You would better use an index and IDocList.Del().
And don't forget to make a "downto" loop, or break the loop on match.

Online

Board footer

Powered by FluxBB