[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Class properties
Each class can have its own properties. A property looks out like a variable in object, but user doesn't have direct access to variable. Input/output can be redirected trough a procedure or function.
This example shows:
Result of this example is the same as below. But procedure can control the value that is passed to the variable and verify it.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Class properties
Each class can have its own properties. A property looks out like a variable in object, but user doesn't have direct access to variable. Input/output can be redirected trough a procedure or function.
type TTest = class
private
propX : integer;
propY : integer;
public
property x : integer read x write x;
property y : integer read y write y;
end;
var Test : TTest;
begin
Test.x := 1;
Test.y := 2;
writeln(Test.x);
writeln(Test.y);
end.
This example shows:
1 2This is easy example. values of properties are stored directly to variables. But there is one big advantage redirecting data trough function.
type TTest = class
private
propX : integer;
propY : integer;
function ReadInt (index : cardinal) : integer;
procedure WriteInt (index : cardinal; value : integer);
public
property x : integer index 1 read ReadInt write WriteInt;
property x : integer index 2 read ReadInt write WriteInt;
end;
function TTest.ReadInt (index : cardinal) : integer;
begin
case index of
1 : ReadInt:=propX;
2 : ReadInt:=propY;
end;
end;
procedure TTest.WriteInt (index : cardinal; value : integer);
begin
case index of
1 : propX:=value;
2 : propY:=value;
end;
end;
var Test : TTest;
begin
Test.x := 1;
Test.y := 2;
writeln(Test.x);
writeln(Test.y);
end.
Result of this example is the same as below. But procedure can control the value that is passed to the variable and verify it.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
