You are not logged in.
Hello,
Assuming the mybaby example in the documentation
was implemented so that Male babies go to a separate table
and Females to their own table.
Since both TSQLBabyMale and TSQLBabyFeMale
have a similar structure, one would like to write
generic code to add a baby to the correct
table depending on its gender.
However, in the call:
myBaby.add_Baby (myBaby, Name_,Address_,BirthDate_);
the whole instance of the table, myBaby, is sent as a parameter.
Is this the way to do it?
Or am I missing something...
Much obliged for your kind help,
Sami
---
Here's the full simplified code:
TSQLBaby = class(TSQLRecord)
private
fName: RawUTF8;
fAddress: RawUTF8;
fBirthDate: TDateTime;
published
property Name: RawUTF8 read fName write fName;
property Address: RawUTF8 read fAddress write fAddress;
property BirthDate: TDateTime read fBirthDate write fBirthDate;
Procedure add_Baby (Const Name_,Address_:String;Const BirthDate_: TDateTime);
end;
TSQLBabyMale = class(TSQLBaby)
end;
TSQLBabyFeMale = class(TSQLBaby)
end;
Procedure TSQLBaby.add_Baby (Const myBaby:TSQLBaby;
Const Name_,Address_:String;Const BirthDate_: TDateTime);
BEGIN
myBaby.Name: := Name_;
myBaby.Address: := Address_;
// now can add record
globalClient.Add(myBaby, true);
END;
Procedure add_new_baby (Const Gender_, Name_,Address_:String;Const BirthDate_: TDateTime);
Var
myBaby :TSQLBaby ;
BEGIN
If Gender ='Male' Then
myBaby := TSQLBabyMale
Else
myBaby := TSQLBabyFeMale;
myBaby.add_Baby (myBaby, Name_,Address_,BirthDate_);
FreeAndNil(myBaby);
END;
Offline
You could do that this way, of course.
IMHO there is no big advantage of separating into two tables, but in some cases (e.g. for UI generation), it could make sense.
The main demo has indeed very close fields, and each table has its own tab on the UI ribbon.
About your code, you may go that way, but with some precisions:
1. define
type TSQLBabyClass = class of TSQLBaby;
2. before creating a "baby" use the class, and create the instance:
Procedure add_new_baby (Const Gender_, Name_,Address_:String;Const BirthDate_: TDateTime);
Var
myBaby :TSQLBaby ;
myBabyClass: TSQLBabyClass;
BEGIN
If Gender ='Male' Then
myBabyClass := TSQLBabyMale
Else
myBabyClass := TSQLBabyFeMale;
myBaby := myBabyClass.Create;
try
myBaby.add_Baby (myBaby, Name_,Address_,BirthDate_);
free
FreeAndNil(myBaby);
end;
END;
3. you may have defined the add_Baby method as class procedure
4. you may even better create a custom constructor instead of this add_baby
Take a look at the main SynFile demo, and the part detailing it in the SAD document.
There are some tricks also in this part.
But it's a good start!
Thanks for your interest
Offline