Forum > Beginners
My helper removes original helpers
Artur2024:
This work
--- 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+}{$modeswitch TypeHelpers} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type //HString = type helper for string // function MyClear(): String; //end; { TForm1 } TForm1 = class(TForm) procedure FormClick(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} //function HString.MyClear(): String;//begin// Result := '';//end; { TForm1 } procedure TForm1.FormClick(Sender: TObject);var s: string;begin s := 'aaa'; ShowMessage(s.Replace('a', 'A'));end; end.
When I add my helpers, this code not work :(
"Replace" is: unit1.pas(46,17) Error: Illegal qualifier
--- 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+}{$modeswitch TypeHelpers} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type HString = type helper for string function MyClear(): String; end; { TForm1 } TForm1 = class(TForm) procedure FormClick(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} function HString.MyClear(): String;begin Result := '';end; { TForm1 } procedure TForm1.FormClick(Sender: TObject);var s: string;begin s := 'aaa'; ShowMessage(s.Replace('a', 'A'));end; end.
How can I add my own helpers?
jamie:
you can inherit from TStringHelper.
If memory serves, I think you specify TStringHelper instead of string.
I would need to look that up.
Blaazen:
https://wiki.freepascal.org/Helper_types
--- Quote ---By default, only one helper type can be active for a type at the same time. This helper is always the last one that is in scope (which follows the usual rules of Pascal).
--- End quote ---
jamie:
--- 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";}};} --- TMyStringHelper = type helper(TStringHelper) for Ansistring // end;
Artur2024:
Thank you, now the code works :D
--- 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+}{$modeswitch TypeHelpers} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type TMyStringHelper = type helper(TStringHelper) for Ansistring function MyClear(): String; end; { TForm1 } TForm1 = class(TForm) procedure FormClick(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} function TMyStringHelper.MyClear(): String;begin Result := '';end; { TForm1 } procedure TForm1.FormClick(Sender: TObject);var s: string;begin s := 'aaa'; ShowMessage(s.Replace('a', 'A')); s := s.MyClear();end; end.
Navigation
[0] Message Index
[#] Next page