TObject

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

TObject

Carsten Bager
A have a unit (uSafeVar) with an object. The object has a property "data" of the type
"SafeVar_typ"

If I do this it works (wSafeVar is called)

var
  p:SafeVar_typ;
begin
  p.d:=111;
  SafeVar.data:=p;
end


If I do this it dos not work but it compiles (wSafeVar is not called)

begin
  SafeVar.data.d:=111;
end;

If I do a read like this ( after the line SafeVar.data.d:=111;)
Writeln(SafeVar.data.d);
the output is 111

The compiler is
Free Pascal Compiler version 2.2.2 [2008/08/26] for arm

Of course I could create a property fore every variable I want to save, but in the real
program it is a big record, so I thought this was a smart way to get around this.
Is it in fact an error that it compiles?


Carsten


--------------------------------------------------------------------------
Unit uSafeVar;
{$mode objfpc}


Interface

Uses
  sysutils,
  Classes;

Type
  SafeVar_typ = packed record
      d:byte;
  end;

  TSafeVar=class(TObject)
    Private
      hSafeVar:SafeVar_typ;
      f:file of SafeVar_typ;
      Procedure wSafeVar(data:SafeVar_typ);
    Public
      constructor create;
      destructor destroy; override;
      property data:SafeVar_typ read hSafeVar write wSafeVar;
    Published
  end;



var
  SafeVar:TSafeVar;

implementation

constructor TSafeVar.create;
Begin
  inherited Create;
  fillchar(hSafeVar,sizeof(SafeVar_typ),chr(0));
  assign(f,SafeVar_dat);
  if fileexists(SafeVar_dat) then
  begin
    reset(f);
    read(f,hSafeVar);
  end else
  begin
    rewrite(f);
    Write(f,hSafeVar);
  end;
End;

destructor TSafeVar.destroy;
Begin
  inherited destroy;
  close(f);
End;

Procedure TSafeVar.wSafeVar(data:SafeVar_typ);
Begin
  hSafeVar:=data;
  seek(f,0);
  Write(f,data);
End;

initialization
  SafeVar:=tSafeVar.create;
finalization
  SafeVar.free;
End.
------------------------------------------

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TObject

Joao Morais
Carsten Bager wrote:

> A have a unit (uSafeVar) with an object. The object has a property "data" of the type
> "SafeVar_typ"
>
> If I do this it works (wSafeVar is called)
>
> var
>   p:SafeVar_typ;
> begin
>   p.d:=111;
>   SafeVar.data:=p;

Here the Data setter is called, ie, wSafeVar, because you are assigning
a new value to it.

> end
>
>
> If I do this it dos not work but it compiles (wSafeVar is not called)
>
> begin
>   SafeVar.data.d:=111;

Here the Data getter is called, ie, hSafeVar, because you are reading a
value from it.

Use a function as a getter instead of a class member. Btw getters and
setters have not the same syntax, you need to create two different methods.

--
Joao Morais


> end;
>
> If I do a read like this ( after the line SafeVar.data.d:=111;)
> Writeln(SafeVar.data.d);
> the output is 111
>
> The compiler is
> Free Pascal Compiler version 2.2.2 [2008/08/26] for arm
>
> Of course I could create a property fore every variable I want to save, but in the real
> program it is a big record, so I thought this was a smart way to get around this.
> Is it in fact an error that it compiles?
>
>
> Carsten
>
>
> --------------------------------------------------------------------------
> Unit uSafeVar;
> {$mode objfpc}
>
>
> Interface
>
> Uses
>   sysutils,
>   Classes;
>
> Type
>   SafeVar_typ = packed record
>       d:byte;
>   end;
>
>   TSafeVar=class(TObject)
>     Private
>       hSafeVar:SafeVar_typ;
>       f:file of SafeVar_typ;
>       Procedure wSafeVar(data:SafeVar_typ);
>     Public
>       constructor create;
>       destructor destroy; override;
>       property data:SafeVar_typ read hSafeVar write wSafeVar;
>     Published
>   end;
>
>
>
> var
>   SafeVar:TSafeVar;
>
> implementation
>
> constructor TSafeVar.create;
> Begin
>   inherited Create;
>   fillchar(hSafeVar,sizeof(SafeVar_typ),chr(0));
>   assign(f,SafeVar_dat);
>   if fileexists(SafeVar_dat) then
>   begin
>     reset(f);
>     read(f,hSafeVar);
>   end else
>   begin
>     rewrite(f);
>     Write(f,hSafeVar);
>   end;
> End;
>
> destructor TSafeVar.destroy;
> Begin
>   inherited destroy;
>   close(f);
> End;
>
> Procedure TSafeVar.wSafeVar(data:SafeVar_typ);
> Begin
>   hSafeVar:=data;
>   seek(f,0);
>   Write(f,data);
> End;
>
> initialization
>   SafeVar:=tSafeVar.create;
> finalization
>   SafeVar.free;
> End.
> ------------------------------------------
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TObject

Carsten Bager
>
> Here the Data getter is called, ie, hSafeVar, because you are reading a
> value from it.
>
> Use a function as a getter instead of a class member. Btw getters and
> setters have not the same syntax, you need to create two different methods.

Was it this you meant? Now I cannot compile. If I change rSafeVar (In the
property declaration) to hSafeVar it compiles.

Carsten

-------------------------------------------------
program safevartest;
{$mode objfpc}

Uses
  sysutils;     {System}


Type
  SafeVar_typ = packed record
      week:byte;
  end;

  TSafeVar=class(TObject)
    Private
      hSafeVar:SafeVar_typ;
      Procedure wSafeVar(data:SafeVar_typ);
      Function rSafeVar(data:SafeVar_typ):SafeVar_typ;
    Public
      constructor create;
      destructor destroy; override;
      property data:SafeVar_typ read rSafeVar write wSafeVar;
    Published
  end;



var
  SafeVar:TSafeVar;

constructor TSafeVar.create;
Begin
  inherited Create;
  fillchar(hSafeVar,sizeof(SafeVar_typ),chr(0));
End;

destructor TSafeVar.destroy;
Begin
  inherited destroy;
End;

Procedure TSafeVar.wSafeVar(data:SafeVar_typ);
Begin
  WriteLn('Write');
End;

Function TSafeVar.rSafeVar(data:SafeVar_typ):SafeVar_typ;
Begin
  WriteLn('Read');
End;

Begin
  SafeVar:=TSafeVar.create;
  SafeVar.data.week:=1;
  SafeVar.free;
End.
Med venlig hilsen
Carsten Bager

BEAS A/S
Brørupvænget 10
DK-7650 Bøvlingbjerg
Tlf. : +45 9788 5222 Fax : +45 9788 5434
www.beas.dk


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TObject

Joao Morais
Carsten Bager wrote:

>> Here the Data getter is called, ie, hSafeVar, because you are reading a
>> value from it.
>>
>> Use a function as a getter instead of a class member. Btw getters and
>> setters have not the same syntax, you need to create two different methods.
>
> Was it this you meant? Now I cannot compile. If I change rSafeVar (In the
> property declaration) to hSafeVar it compiles.
>
> [...]
>
>   TSafeVar=class(TObject)
>     Private
>       hSafeVar:SafeVar_typ;
>       Procedure wSafeVar(data:SafeVar_typ);
>       Function rSafeVar(data:SafeVar_typ):SafeVar_typ;

Change to

       Function rSafeVar: SafeVar_typ;

--
Joao Morais


>     Public
>       constructor create;
>       destructor destroy; override;
>       property data:SafeVar_typ read rSafeVar write wSafeVar;
>     Published
>   end;
>
>
>
> var
>   SafeVar:TSafeVar;
>
> constructor TSafeVar.create;
> Begin
>   inherited Create;
>   fillchar(hSafeVar,sizeof(SafeVar_typ),chr(0));
> End;
>
> destructor TSafeVar.destroy;
> Begin
>   inherited destroy;
> End;
>
> Procedure TSafeVar.wSafeVar(data:SafeVar_typ);
> Begin
>   WriteLn('Write');
> End;
>
> Function TSafeVar.rSafeVar(data:SafeVar_typ):SafeVar_typ;
> Begin
>   WriteLn('Read');
> End;
>
> Begin
>   SafeVar:=TSafeVar.create;
>   SafeVar.data.week:=1;
>   SafeVar.free;
> End.
> Med venlig hilsen
> Carsten Bager
>
> BEAS A/S
> Brørupvænget 10
> DK-7650 Bøvlingbjerg
> Tlf. : +45 9788 5222 Fax : +45 9788 5434
> www.beas.dk
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TObject

Carsten Bager
> Change to
>
>        Function rSafeVar: SafeVar_typ;
Joao Morais

Sorry about that. I pasted the wrong code in the email. I have
already changed that. It does still not compile after the change.
Carsten


Med venlig hilsen
Carsten Bager

BEAS A/S
Brørupvænget 10
DK-7650 Bøvlingbjerg
Tlf. : +45 9788 5222 Fax : +45 9788 5434
www.beas.dk


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal