0

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.

1
  • Please add what you have tried and what is the expected output. Commented Dec 1, 2024 at 17:44

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.