I want to store this string expression:
oField.displayname+'='+varTostr(fieldbyname(ofield.fieldame).value)
where ofield is TField of dataset in TComponent.Tag. How can I do this?
I am using FreePascal.
I want to store this string expression:
oField.displayname+'='+varTostr(fieldbyname(ofield.fieldame).value)
where ofield is TField of dataset in TComponent.Tag. How can I do this?
I am using FreePascal.
The Tag field of a TComponent is of type PtrInt, which is large enough to store a pointer, including the memory address of a string. You can use the Tag to store the address of a dynamically allocated string.
procedure TForm1.FormCreate(Sender: TObject);
var
MyString: PChar;
begin
MyString := StrAlloc(100);
StrPCopy(MyString, 'Hello, Free Pascal!');
Label1.Tag := PtrInt(MyString);
Edit1.Text := PChar(Label1.Tag);
StrDispose(PChar(Label1.Tag));
end;
Ensure that the memory is always properly deallocated to avoid leaks or dangling pointers
You asked this same question in the FreePascal forum, so I'll post here the same example I gave you there.
https://forum.lazarus.freepascal.org/index.php/topic,69500.msg539938.html#msg539938
// to store a new string... var tag: Pointer; tag := nil; string(tag) := 'yourstringhere'; TButton.Tag := PtrInt(tag);// to retrieve the stored string... var tag: Pointer; tag := Pointer(TButton.Tag); Somestring := string(tag);// to update the stored string... var tag: Pointer; tag := Pointer(TButton.Tag); string(tag) := 'yourstringhere'; TButton.Tag := PtrInt(tag);// to release the stored string when done using the Tag... var tag: Pointer; tag := Pointer(TButton.Tag); string(tag) := ''; TButton.Tag := 0;[/code]