Forum > General

storing string

<< < (3/4) > >>

Remy Lebeau:

--- Quote from: Fibonacci on December 02, 2024, 05:21:17 pm ---Project project1 raised exception class 'External: ACCESS VIOLATION' with message:
Access violation writing to address $00000001001596D4.

--- End quote ---

Sorry, I forgot to initialize the PtrInt to 0 before assigning the string value to it.  I have updated my earlier example.

egsuh:



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
And back in the day I would use...


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version