#1 2014-09-22 02:53:26

qch1989
Member
Registered: 2014-09-18
Posts: 2

need function AddValue and property Value in TDocVariant

I use TDocVariant as parameter and return type of a public function of my Form. I need function AddValue and property Value in TDocVariant to get and set attributes value.

I can't use the form: doc.name, cause the "name" is from a variable.

I know I can type-cast it to TDocVariantData to use them. but I think it's better i can use it in TDocVariant directly. Do I miss some handy way to do it?

here is my test code for AddValue:

procedure TForm1.btnOpenformClick(Sender: TObject);
var
  re: Variant;
begin
  re := TDocVariant.New;
  // edt1.text = 'abc'
  TDocVariantData(re).AddValue(edt1.text, '123');
  ShowMessage(re.abc);
end;

Offline

#2 2014-09-22 10:29:45

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,580
Website

Re: need function AddValue and property Value in TDocVariant

This is not possible, because we can have object

{
  "Value": 10,
  "AddValue": "this is some additional value"
}

And how to make difference between setting property Value and calling getter Value?

Offline

#3 2014-09-22 18:31:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,007
Website

Re: need function AddValue and property Value in TDocVariant

Just use local var TDocVariant instead of variant.

See the doc about direct access without late bindings.

Offline

#4 2014-09-22 19:51:51

qch1989
Member
Registered: 2014-09-18
Posts: 2

Re: need function AddValue and property Value in TDocVariant

Thanks for mpv and ab's quick replies.

ab wrote:

Just use local var TDocVariant instead of variant.

I read the doc, it seems using TDocVariantData to access directly. so my test code should be:

procedure TForm1.btnOpenformClick(Sender: TObject);
var
  re: TDocVariantData;
begin
  re.Init;
  // edt1.text = 'abc'
  re.AddValue(edt1.text, '123');
  ShowMessage(re.Value['abc']);
end;

Am I right ?

and that means I have to give up the form re.abc to get the value. I still like this straightforward form.

To get or set value from a variable as name, Is that possible to let TDocVariant support default index property, then we can use re[edt1.text] to set or get value ?

Offline

#5 2014-09-22 23:00:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,007
Website

Re: need function AddValue and property Value in TDocVariant

Yes.

Offline

Board footer

Powered by FluxBB