Problème avec TPersistent
Salut tout le monde,
J'ai un petit problème avec un TPersistent.
Je m'explique:
J'ai deux composant: TCustomMachine qui hérite de TPersistent et TFlow qui hérite de TComponent:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| TCustomMachine=class(TPersistent)
private
FCodeIMMO: Word;
FIdHum:integer;
procedure SetCodeIMMO(const Value: Word);
procedure CheckCodeImmoValid(CodeImmo:integer);
protected
FOwner: TComponent;
function GetOwner: TPersistent; override;
public
constructor Create(Aowner: TComponent); virtual;
procedure Assign(Source : TPersistent); override;
published
property CodeIMMO:Word read FCodeIMMO write SetCodeIMMO;
end;
{ TCustomMachine }
procedure TCustomMachine.Assign(Source: TPersistent);
begin
if Source is TCustomMachine then
TCustomMachine(Source).FCodeIMMO := FCodeIMMO
else
inherited;
end;
constructor TCustomMachine.Create(Aowner: TComponent);
begin
inherited create;
FOwner:=AOwner;
end;
function TCustomMachine.GetOwner: TPersistent;
begin
Result:=FOwner;
end;
procedure TCustomMachine.SetCodeIMMO(const Value: Word);
begin
if value<>FCodeIMMO then
begin
FCodeIMMO:=Value;
CheckCodeImmoValid(value);
end;
end;
procedure TCustomMachine.CheckCodeImmoValid(CodeIMMO:integer);
var
MyQuery:TMyQuery;
begin
FIdHum:=-1;
MyQuery:=TMyQuery.Create(nil);
try
with MyQuery do
begin
Connection:=TCustomFlow(FOwner).FMySQLConnection;
SQL.Clear;
SQL.Add('select id_hum,alias from machine.numero_hum where '+
'code_immo=:CodeIMMO');
Params[0].Value:=CodeIMMO;
Execute;
if Recordcount<>0 then
begin
First;
FIdHum:=Fields[0].Value;
end;
end;
finally
MyQuery.Free;
end;
end; |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
TCustomFlow=class(TComponent)
private
FMySQLConnection: TCustomMyConnection;
FMachine:TMachine;
procedure SetMachine(const Value: TMachine);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Connection:TCustomMyConnection read FMySQLConnection write
FMySQlConnection;
property Machine:TMachine read FMachine write SetMachine;
end;
constructor TCustomFlow.Create(AOwner: TComponent);
begin
inherited;
FMachine:=TMachine.Create(self);
end;
destructor TCustomFlow.Destroy;
begin
FMachine.Free;
inherited;
end;
procedure TCustomFlow.SetMachine(const Value: TMachine);
begin
FMachine.Assign(Value);
end; |
Tout fonctionne parfaitement dans l'inspecteur d'object.
Mais quand je compile mon logiciel, la méthodeTCustomMachine.SetCodeIMMO
est appelée, alors que je n'ai pas encore la connection dans FMysqlConnection
et forcement j'ai mon application qui plante dans la procédure TCustomMachine.CheckCodeImmoValid.
Il faut bien comprendre que toutes les interconnections sont définies au moment du design et que je n'ai aucun code dynamique qui change les property.
La seule solution que j'ai trouvé est de tester avant l'éxécution de la procédure si FMySQLConnection est différent de Nil. Mais cela m'oblige à rajouter une méthode dans TCustomFlow pour "rafraichir" mon objet FMachine, une fois que ma connection est valide.
Y a t il un moyen de résoudre ce problème plus facilement ?
J'espere que mes explications sont claires.
Cordialement