Forum > General
I found something clever! short way to define a RECORD with a couple of values.
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";}};} ---unit Unit1; {$mode objfpc}{$H+}{$modeSwitch AdvancedRecords} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type { TForm1 } TMyRecord = Record x,Y:Integer; Function fReadXY(xx,yy:Integer):TMyRecord; inline; Public property XY[xx,yy:integer]:TMyRecord read freadXY; default; end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end; var Form1: TForm1; implementation{$R *.lfm}Function TMyRecord.fReadXY(xx,yy:Integer):TMyRecord;Begin x := xx; y := yy; result := Self;End;{ TForm1 }Procedure Displayit(AR:TMyRecord); overload;begin Form1.Caption := Ar.x.Tostring+','+AR.y.Tostring;end;procedure TForm1.Button1Click(Sender: TObject);Var Y,Z:TMyrecord;begin Y[10,40]; // this works; DisplayiT(Z[1000,45]);// This works as valueend; end. Now I can Add the constructor in there so that we can create parametrize instances, but I found this interesting because I can assign and test it inline with an if statement I believe, Haven't don't that yet
jamie:
An update to this use.
The records I have do have Constructors already and I found that I did not need to add a function, I can call the constructor and it works the same it seems. so no need to add a function.
--- 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";}};} ---// within the ReocordProperty C[x,y:Integer]:TMyPoint Read Create; default;
With that you can shorthand the call to the Constructor.
so in code
--- 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";}};} ---Var A,B:TMyPoint;Begin A[x,y]; B:= A[x,y] //on so on.
This saves me tons of typing !
Also, I noticed it only works with a READ accessor, Write accessor does not seem to be accepted.
JdeHaan:
Nice idea!
--- 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";}};} ---program records;{$mode delphi} TLocal = record Name: ShortString; Level: Integer; Captured: Boolean; constructor Create(aName: String; aLevel: Integer; aCaptured: Boolean); property Value[aName: String; aLevel: Integer; aCaptured: Boolean]:TLocal read Create; default; end; constructor TLocal.Create(aName: String; aLevel: Integer; aCaptured: Boolean);begin Name := aName; Level := aLevel; Captured := aCaptured;end; var Local: TLocal; begin Local['Name', 42, True]; Writeln(Local.Name, ', ', Local.Level, ', ', Local.Captured);end.
It works with ShortString fields, but not with fields of type String.
jamie:
Managed types, I never tried that. I normally use primitive types.
you could try to prefix the NAME with Self.NAME := aName;
jamie:
You may want to try using the DEFAULT(Self) while in the constructor before setting the strings.
Navigation
[0] Message Index
[#] Next page