Stefano Losi
Stefano Losi

Reputation: 729

Invoking object properties in Delphi

I admit I'm not a Delphi expert, so I need some advice. I have a pre-built class with this definition

  TS7Helper = class
  private
    function GetInt(pval: pointer): smallint;
    procedure SetInt(pval: pointer; const Value: smallint);
    function GetWord(pval: pointer): word;
    procedure SetWord(pval: pointer; const Value: word);
    function GetDInt(pval: pointer): longint;
    procedure SetDInt(pval: pointer; const Value: longint);
    function GetDWord(pval: pointer): longword;
    procedure SetDWord(pval: pointer; const Value: longword);
    function GetDateTime(pval: pointer): TDateTime;
    procedure SetDateTime(pval: pointer; const Value: TDateTime);
    function GetReal(pval: pointer): single;
    procedure SetReal(pval: pointer; const Value: single);
    function GetBit(pval: pointer; BitIndex: integer): boolean;
    procedure SetBit(pval: pointer; BitIndex: integer; const Value: boolean);
  public
    procedure Reverse(pval : pointer; const S7Type : TS7Type);
    property ValBit[pval : pointer; BitIndex : integer] : boolean read GetBit write SetBit;
    property ValInt[pval : pointer] : smallint read GetInt write SetInt;
    property ValDInt[pval : pointer] : longint read GetDInt write SetDInt;
    property ValWord[pval : pointer] : word read GetWord write SetWord;
    property ValDWord[pval : pointer] : longword read GetDWord write SetDWord;
    property ValReal[pval : pointer] : single read GetReal write SetReal;
    property ValDateTime[pval : pointer] : TDateTime read GetDateTime write SetDateTime;
  end;

Var
  S7 : TS7Helper;

procedure TS7Helper.SetInt(pval: pointer; const Value: smallint);
Var
  BW : packed array[0..1] of byte absolute value;
begin
  pbyte(NativeInt(pval)+1)^:=BW[0];
  pbyte(pval)^:=BW[1];
end;

(I cut some code, so don't look for the implementation clause, etc... the helper class is compiling ok....)

Trivially, I want to invoke the SetInt property (as stated in the class documentation)... but the following code gives me an error "Cannot access private symbol TS7Helper.SetInt".

S7.SetInt(@MySnap7Array[i * 2], gaPlcDataScrittura[i]);

What am I doing wrong ?

Upvotes: 0

Views: 162

Answers (2)

JRL
JRL

Reputation: 3401

In Delphi,

A private member is invisible outside of the unit or program where its class is declared.

Note: "program" refers to files starting with program keyword (usually the .dpr file), not to the project as a whole.

So you can only call TS7Helper.SetInt from the same unit where TS7Helper class is declared.

Otherwise, @DmLam answer is the correct way to solve it.

Upvotes: 0

DmLam
DmLam

Reputation: 136

SetInt and GetInt is the getter and setter for ValInt property as stated in the definition of ValInt. So you shoud use S7.ValInt like

S7.ValInt[@MySnap7Array[i * 2]] := gaPlcDataScrittura[i];

Upvotes: 2

Related Questions