Forum > FPC development

Lazarus, helper properties of freepascal

(1/4) > >>

staratel20:
Good day.

Often needed some List with additional properties. Easiest example - TListBox with boolean property Checked, and for example we need also boolean property Founded. So, very abstract(and only for example), can looks like:

--- Quote ---TCheckedList=class
  property Item[AIndex:integer]:TStringList read GetItem;default;
  property Checked[AIndex:integer]:TBoolList read GetChecked;
  property Founded[AIndex:integer]:TBoolList read GetState;
end;

MyStr :TCheckedList;

--- End quote ---
after that we access to properties:


--- 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";}};} --- MyStr.Checked[i];   MyStr.Founded[i];   // .. and so on. looks poor  
What I propose. Syntax, something like:


--- Quote ---TCheckedList=class
  property Item[AIndex:integer]:TStringList read GetItem;default;
  property Checked[AIndex:integer]:TBoolList read GetChecked;attached;  // attached to default property
  property Founded[AIndex:integer]:TBoolList read GetState;attached;  // attached to default property
end;
--- End quote ---

and after that we can write the same like:


--- 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";}};} ---MyStr[i].Checked;   // will call GetChecked[i]MyStr[i].Founded;   // will call GetFounded[i] 
.. and so on. looks good  :)

Interest what you think about it.

Thaddy:
You can use a type helper for the Item type to achieve just that!.
I'll see if I can come up with a small example later.

staratel20:
I would thanks a lot)

staratel20:
I read carefully this article(and continue discover it):
http://wiki.freepascal.org/Helper_types

but not sure that is possible realize what I'm talking about without changing to FPC core. At first if I'm extend TStringList with class TCheckedStringList - property Items are already busy. But I don't need changing behaviour of Items-property. So more deployed I need next behaviour:


--- Quote ---s:string;
s:=MyStr;  // will call Items of class TStringList
MyStr.Checked;   // will call GetChecked of TCheckedStringList
MyStr.Founded;   // will call GetFounded of TCheckedStringList

--- End quote ---

Also I think that my suggested syntax is more simple and intuitive. Can I hope that something kind of this will be realized in next FPC versions?

skalogryz:
why TCheckedList returns lists ? (TStringList, TBoolList?)
Is it some sort of 2d-array... a list of lists?

But if you need to add these properties to TListBox, this is what you could do using helpers

--- 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";}};} ---type  TListBoxChecked = class helper for TListBox  protected    function GetChecked(i: integer): Boolean;  public    property Checked[i: integer]: Boolean read GetChecked;  end;...function TListBoxChecked.GetChecked(i: integer): Boolean;begin  if not Assigned(Objects[i]) then Result:=false  else Result:=TMyObject(Objects[i]).checked;end; The actual implementation of .GetChecked() (or SetChecked()) depends on how you store "checked" value for an item within TListBox.

Navigation

[0] Message Index

[#] Next page

Go to full version