storing string lazarus / 21-General ---------------------------------------------------------------------- Medhome · 2024-12-01 18:08 Hi All How to store a  string into tag propertie of an object for example   in TButton.tag Regards Med ---------------------------------------------------------------------- cdbc · 2024-12-01 18:18 Hi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TButton.tag:= ptrint(strnew('yourstringhere'));Regards Benny ---------------------------------------------------------------------- Medhome · 2024-12-01 18:22 Thanks. But How to retrieve ? Med ---------------------------------------------------------------------- cdbc · 2024-12-01 18:27 Hi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var p: pchar;begin  p:= pchar(TButton.Tag);  Caption:= string(p);  strDispose(p);end; Regards Benny ---------------------------------------------------------------------- Remy Lebeau · 2024-12-02 15:41 Here is a slightly cleaner way without resorting to StrNew()/StrDispose(): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // to store a new string...var tag: Pointer;tag := nil;string(tag) := 'yourstringhere';TButton.Tag := PtrInt(tag); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // to retrieve the stored string...var tag: Pointer;tag := Pointer(TButton.Tag);Somestring := string(tag); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // to update the stored string...var tag: Pointer;tag := Pointer(TButton.Tag);string(tag) := 'yourstringhere';TButton.Tag := PtrInt(tag); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // to release the stored string when done using the Tag...var tag: Pointer;tag := Pointer(TButton.Tag);string(tag) := '';TButton.Tag := 0; ---------------------------------------------------------------------- Fibonacci · 2024-12-02 16:21 Quote from: Remy Lebeau on December 02, 2024, 04:41:09 pmThere is a cleaner way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var tag: PtrInt;string(tag) := 'yourstringhere';TButton.Tag := tag; Project project1 raised exception class 'External: ACCESS VIOLATION' with message: Access violation writing to address $00000001001596D4.  In file 'x86_64.inc' at line 1255: decl       {$ifdef win64} (%rcx) {$else} (%rdi) {$endif} ---------------------------------------------------------------------- Thaddy · 2024-12-02 16:45 Maybe:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$modeswitch typehelpers}{$H+}{$M+}uses classes; type  TTagString = type helper for PtrInt // tag is ptrint.  strict private    function  GetString:String;inline;    procedure SetString(const value:string);inline;  published    property AsString:String read GetString write SetString;  end;  function  TTagString.GetString:String;inline; begin   Result := string(self); end;     procedure TTagString.SetString(const value:string);inline; begin   string(self) := value; end; var  a:TComponent;  begin  a:= TComponent.Create(nil); // anything with a tag  a.tag.AsString := 'Hello, tag';  writeln(a.tag.AsString);  a.free;end.More Pascal code, once, less fuss.  (the generated assembler code is minimal!) Anyway, it is not wise to use the tag as a permanent solution in production code. But this code doesn't crash...and does the job... ---------------------------------------------------------------------- domasz · 2024-12-02 18:04 How about? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type TButton = class(StdCtrls.TButton)public  Str: String;end;  and then: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Button1.Str := 'My String'; ---------------------------------------------------------------------- Thaddy · 2024-12-02 18:12 - doesn't set caption. - doesn't cover all components. - doesn't exist. ---------------------------------------------------------------------- domasz · 2024-12-02 18:23 Quote from: Thaddy on December 02, 2024, 07:12:38 pm- doesn't set caption. - doesn't cover all components. - doesn't exist. 1) It's not supposed to set Caption. Instead of Tag it uses Str, so Tag can be used for integers, like intended. 2) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  TComponent = class(Classes.TComponent) public   Str: String; end; 3) No idea what you mean ---------------------------------------------------------------------- Remy Lebeau · 2024-12-02 20:46 Quote from: Fibonacci on December 02, 2024, 05:21:17 pmProject project1 raised exception class 'External: ACCESS VIOLATION' with message: Access violation writing to address $00000001001596D4. Sorry, I forgot to initialize the PtrInt to 0 before assigning the string value to it.  I have updated my earlier example. ---------------------------------------------------------------------- egsuh · 2024-12-03 08:13 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TButton = class(StdCtrls.TButton)  public     TextTag : string;  end;    { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin   Button1.TextTag:= 'This is my text tag';end; procedure TForm1.Button1Click(Sender: TObject);begin  showmessage(button1.TextTag);end; end. ---------------------------------------------------------------------- Thaddy · 2024-12-03 08:38 Yes, but that requires inheritance. My helper solution solves it for any component, all of them. Without the need to modify the inheritance tree. ---------------------------------------------------------------------- silvercoder70 · 2024-12-04 12:00 And back in the day I would use... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RefString(const S: string): Pointer;var  Local: string;begin  Local := S;  Result := Pointer(Local);  Pointer(Local) := nil;end; procedure ReleaseString(P: Pointer);var  Local: string;begin  Pointer(Local) := P;  end;  You would have to look at "Delphi in a Nutshell" page 60 (?) for the explanation. ---------------------------------------------------------------------- ASerge · 2024-12-04 22:06 I don't really like all the solutions from above. It takes a lot of effort to figure out why it works. And repeated conversions of types. In some solutions, they forgot about freeing up memory, apparently they tested it only on constants. In my opinion, there is already a container for strings, it's easy enough to use it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls; type  TForm1 = class(TForm)    Button1: TButton;  Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    FTagHolder: TStringList;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  Button1.Tag := FTagHolder.Add('Some string');end; procedure TForm1.Button2Click(Sender: TObject);begin  Caption := FTagHolder[Button1.Tag];end; procedure TForm1.FormCreate(Sender: TObject);begin  FTagHolder := TStringList.Create;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FTagHolder.Free;end; end. ---------------------------------------------------------------------- Wesbat · 2024-12-06 03:51 Quote from: Remy Lebeau on December 02, 2024, 04:41:09 pmHere is a slightly cleaner way without resorting to StrNew()/StrDispose(): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var tag: Pointer;tag := nil;string(tag) := 'yourstringhere';TButton.Tag := PtrInt(tag); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var tag: Pointer;tag := Pointer(TButton.Tag);Somestring := string(tag);...string(tag) := ''; As someone who is still learning, I find this example very instructive into how Pointers operate. Thanks for sharing.