Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-09

TObject

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.
------------------------------------------

Reply by email

Joao Morais
to Carsten Bager
Carsten Bager wrote 10 lines
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.

Show quoted text 7 lines
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. > ------------------------------------------ >

Reply by email

Carsten Bager
to Joao Morais
Show quoted text 6 lines
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

Reply by email

Joao Morais
to Carsten Bager
Carsten Bager wrote 16 lines
> 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 - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >

Reply by email

Carsten Bager
to Joao Morais
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

Reply by email