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

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
2

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

last edited (January 22, 2004) by Mr. Ego, Number of views: 3337, Current Rev: 1

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.