Recent

Author Topic: What is wrong with this object assignment?  (Read 3394 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
What is wrong with this object assignment?
« 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?

Code: [Select]
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
;
Lazarus 3.0/FPC 3.2.2

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: What is wrong with this object assignment?
« Reply #1 on: February 06, 2013, 11:58:24 pm »
Strange.
Isn't there missing:
Code: [Select]
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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: What is wrong with this object assignment?
« Reply #2 on: February 07, 2013, 12:42:19 pm »
I see what you mean.

I thought that setting
Code: [Select]
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:
Code: [Select]
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.
Lazarus 3.0/FPC 3.2.2

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: What is wrong with this object assignment?
« Reply #3 on: February 07, 2013, 01:36:35 pm »
Code: [Select]
  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?
« Last Edit: February 07, 2013, 01:39:11 pm by User137 »

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: What is wrong with this object assignment?
« Reply #4 on: February 07, 2013, 04:15:32 pm »
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.

Code: [Select]
  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?
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018