Forum > Beginners

Procedure with parameters as a parameter of procedure

(1/1)

TomTom:
I have this procedure that I want to use as a argument of another procedure

--- 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";}};} ---procedure TForm1.UpdateGameDetails(aGamesList: TStringList;  aGameIndex: integer; aGameDetail: TGameDetails; aValue:string);var  new_value: string;  i: Integer;  temp_table: TStringDynArray;begin  new_value:='';  temp_table :=SplitString(aGamesList[aGameIndex],';');  temp_table[Ord(aGameDetail)] := aValue;  for i:= low(temp_table) to Length(temp_table) do begin    new_value := new_value+temp_table[i]+';';  end; aGamesList[aGameIndex]:=new_value; aGamesList.SaveToFile('games');end;  
In Lable1.OnClick event:

--- 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";}};} ---procedure TForm1.Label1Click(Sender: TObject);begin  CreateLabelEditBox(Label1,@UpdateGameDetails(list, ListBox1.ItemIndex, gdTitle, edLabelEdit.text));end;  

--- 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";}};} ---procedure TForm1.CreateLabelEditBox(aObject:TControl; aOnEditingDone: TNotifyEvent);begin  if edLabelEdit = nil then begin      edLabelEdit:=TEdit.Create(nil);      edLabelEdit.Left:=aObject.Left;      edLabelEdit.Top:=aObject.top;      edLabelEdit.BorderStyle:=bsNone;      edLabelEdit.Height:=aObject.Height;      edLabelEdit.Width:=aObject.Width;      edLabelEdit.Color:=RGBToColor(100,100,100);      edLabelEdit.OnEditingDone:=aOnEditingDone;      edLabelEdit.Font:=aObject.Font;      edLabelEdit.Parent:=self;      edLabelEdit.SetFocus;   end  else begin      edLabelEdit.Left:=aObject.Left;      edLabelEdit.Top:=aObject.top;      edLabelEdit.Height:=aObject.Height;      edLabelEdit.Width:=aObject.Width;      edLabelEdit.OnEditingDone:=aOnEditingDone;      edLabelEdit.Font:=aObject.Font;      edLabelEdit.SetFocus;   end; end; But it Gives me Error: Variable identifier expected.  Please help :)

winni:
Hi!

Help yourself:

The error message  tell you the line number and the column number, where the error occurs.

So you are able to place the cursor exact where the compiler howls.

In opposite to us.

Winni

dseligo:
It won't work like that.

You have to declare your event like this:

--- 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";}};} ---procedure TForm1.UpdateGameDetails(Sender: TObject);
And you set it like this:

--- 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";}};} ---procedure TForm1.Label1Click(Sender: TObject);begin  CreateLabelEditBox(Label1,@UpdateGameDetails));end;
I don't know what you want to do exactly, but you have to think of another way to provide data to your event (maybe put it in a class when creating control or something like that).

Warfley:
What you are trying to do in functional languages would be called partial application, where you take a function, fix the parameters and get a new function as a result that calls the existing function using those parameters.

In pascal this behavior can be approximated with function references, which are right now only available in the trunk of fpc. The problem is that you want to set an LCL event, and as far as I know they (currently) still use method pointers and not (the more generic) function references.

Thererfore, long story short, you can't do it this way. Potentially, as function references are just an interface internally, you can cast this to a method on that interface and this could work.

Here is how you need to do it right now (with current stable fpc): Create a new class which holds all the parameters for said function as fields. Then create you Method in that class that calls the function with the parameters stored as class fields


--- 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  TUpdateGameDetailsCaller = class  private    FForm: TForm1;    FGamesList: TStringList;    FGameIndex: integer;    FGameDetail: TGameDetails;     FValue:string;  public    constructor Create(AForm: TForm1; GamesList: TStringList; aGameIndex: integer; aGameDetail: TGameDetails; aValue:string);    procedure Call(Sender: TObject);  end; constructor TUpdateGameDetailsCaller.Create(AForm: TForm1; GamesList: TStringList; aGameIndex: integer; aGameDetail: TGameDetails; aValue:string);begin  FForm := AForm;  FGamesList := GamesList;  FGameIndex := aGameIndex;  FGameDetail := aGameDetail;  FValue:string := aValue;end; procedure Call(Sender: TObject);begin  FForm.UpdateGameDetails(FGamesList, FGameIndex, FGameDetail, FValue)end;
And called with:

--- 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";}};} --- CreateLabelEditBox(Label1,@TUpdateGameDetailsCaller.Create(list, ListBox1.ItemIndex, gdTitle, edLabelEdit.text).Call);
How to free the temporary object afterwards is something you can findout yourself. Thats the hard part (because manual memory management sucks)

Navigation

[0] Message Index

Go to full version