Lazarus
Programming => General => Topic started by: vfclists on February 06, 2013, 11:32:34 pm
-
In the code below I am using the variable f as an abbreviation for a field named FDataModule. The idea is to check that the data module exists an display a related form. If the data module does not exist, it is created before.
I thought that by f:= FDataModule following by the Assigned procedure would work as I expected, f would not be nil iif FDataModule was already created. However in this case f is always nil and new forms are always opened.
What is wrong with the code?
procedure TdmCentral.actionExecute(Sender: TObject);
var
f: TdmDateModule;
begin
{
f := FDataModule;
if not Assigned(f) then
begin
f := TdmDataModule.Create(self);
end;
f.DisplayForm;
}
if not Assigned(FDataModule) then
begin
FDataModule := TdmDataModule.Create(self);
end;
FDataModule.DisplayForm;
end
;
-
Strange.
Isn't there missing:
FDataModule := f;
below the construction?
I mean that the TdmDateModule (in variable f) is created again and again but always lost (but not freed) when method ends.
-
I see what you mean.
I thought that setting f:= FDataModule
and creating f would automatically assign FDataModule as well. In effect Assigned(f) would be true if FDataModule was not nil to begin with.
Strange.
Isn't there missing:
FDataModule := f;
below the construction?
I mean that the TdmDateModule (in variable f) is created again and again but always lost (but not freed) when method ends.
-
if not Assigned(FDataModule) then
begin
FDataModule := TdmDataModule.Create(self);
end;
FDataModule.DisplayForm;
The code block looks fine. What else is happening, for instance have you debugged inside DisplayForm()? And how is FDataModule initialized? Can TdmDataModule.Create(self) take TdmCentral as parameter?
-
The question is concerned with the part that is in comments. In any case the semantics were not what I thought they were, but Blaazen pointed out the error.
if not Assigned(FDataModule) then
begin
FDataModule := TdmDataModule.Create(self);
end;
FDataModule.DisplayForm;
The code block looks fine. What else is happening, for instance have you debugged inside DisplayForm()? And how is FDataModule initialized? Can TdmDataModule.Create(self) take TdmCentral as parameter?