When you define a Class (or an Object or Record with methods), I was just wondering if there's any convention as to whether the data fields should appear before the method fields?
I mean like the difference between the two cases in the following trivial example. (edited to make the specific problem clearer)
type TWhatever = Object
private
a,b : Integer;
public
procedure SetWhatever(x,y :Integer);
procedure ShowWhatever;
end;
versus
type TWhatever = Object
public
procedure SetWhatever(x,y :Integer);
procedure ShowWhatever
private
a,b : Integer;
end;
I've always used the convention of listing the data fields first, but to be honest I didn't really think it made any difference. Recently however I came across some code that didn't work (wouldn't compile) unless the data fields were listed first.
I've documented the case here: Delphi 2006 wont allow const parameters of type record within record method?
The summary is that if you put the data fields after the methods, then the code wont compile if the object has any methods that try to pass the given object as "const" parameter. For example something like this won't compile:
procedure TWhatever.SomeMethod( const : w1: TWhatever);
Apparently it will compile on later versions of Delphi, but I've tested it on both Delphi7 and Delphi 2006 and it wont compile on either.
The problem only appears if you do all three things (data fields last, pass object as parameter in method, use the const keyword for this parameter). So you can solve the problem by either removing the const keyword from the parameter or by putting the data fields ahead of the methods.
Anyway, this issue has just made me wonder if there is a convention to be followed here?
EDITED I added this to give a specific code example
The following program fails to compile on both D7 and D2006:
program bugtest;
{$APPTYPE CONSOLE}
uses SysUtils;
type Tob = object
public
procedure setOb(const a,b: integer);
procedure addToOb(const ob1: Tob);
procedure printOb;
private
x,y : Integer;
end;
procedure Tob.setOb(const a,b: integer);
begin
x:=a; y:=b;
end;
procedure Tob.addToOb(const ob1: Tob);
begin
x:=x+ob1.x; y:= y+ob1.y;
end;
procedure Tob.printOb;
begin
writeln(x,' ',y);
end;
var r1,r2: Tob;
begin
r1.setOb(2,3);
r2.setOb(10,100);
r1.addToOb(r2);
r1.printOb;
r2.printOb;
readln;
end.
The following program compiles and runs perfectly on both D7 and D2006:
program bugtest;
{$APPTYPE CONSOLE}
uses SysUtils;
type Tob = object
private
x,y : Integer;
public
procedure setOb(const a,b: integer);
procedure addToOb(const ob1: Tob);
procedure printOb;
end;
procedure Tob.setOb(const a,b: integer);
begin
x:=a; y:=b;
end;
procedure Tob.addToOb(const ob1: Tob);
begin
x:=x+ob1.x; y:= y+ob1.y;
end;
procedure Tob.printOb;
begin
writeln(x,' ',y);
end;
var r1,r2: Tob;
begin
r1.setOb(2,3);
r2.setOb(10,100);
r1.addToOb(r2);
r1.printOb;
r2.printOb;
readln;
end.
Delphi 7 version is: Delphi Personal Version 7.0 build 4.453.
D2006 version is: Borland Delphi for MS Windows. Version 10.0.2288.42451, Update2.
var
clause should precede all procedure&function declarations in every code block. – kludg