Entry
Delphi: How to use the 'this' command (='self') in Delphi?
Dec 12th, 2003 15:38
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 29 April 2002 - 01:58 am ---------------------
Delphi: How to use the 'this' command (='self') in Delphi?
---
The 'this' keyword in C++ maps onto the 'Self' keyword in Object
Pascal.
[book: source: Entsminger, Gary - the way of Delphi - Prentice Hall -
ISBN 0-13-455271-7 - p. xxxii 'Miscellaneous']
---
---
All classes have a hidden field called Self.
Self is a pointer to the instance of the class in memory.
[book: source: - Teach Yourself Borland Delphi 4 in 21 Days - Kent
Reisdorph - SAMS - ISBN 0672312867 -
http://www.amazon.com/exec/obidos/tg/detail/-
/0672312867/qid=1064501349/sr=1-1/ref=sr_1_1/103-6292513-4946208?
v=glance&s=books#product-details]
---
---
The keyword 'Self' is used by you to let the compiler distinguish
between different variables with the *same* name inside a class.
---
So it lets the compiler to distinguish between:
which *same* variable names correspond to class members
and
which *same* variable names correspond to method parameters,
---
To distinguish, you precede that class member variable names with the
'self' keyword, as shown here:
---
// save as Unit1.pas [kn, ri, su, 28-04-2002 18:00:34]
//
unit Unit1;
//
interface
//
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs;
//
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
//
TYPE
CLASSEmployee = CLASS( TObject )
PRIVATE
nameS: STRING[255];
employeeidI: INTEGER;
officenumberI: INTEGER;
salaryD: REAL;
//
PUBLIC
//
CONSTRUCTOR Create( nameS: STRING ); overload;
CONSTRUCTOR Create( nameS: STRING; employeeidI: INTEGER;
officenumberI: INTEGER; salaryD: REAL ); overload;
PROCEDURE PROCEmployeeShowRecord; overload;
PROCEDURE PROCEmployeeShowRecord( xI: INTEGER; yI: INTEGER );
overload;
END;
//
VAR
Form1: TForm1;
defaultidI: INTEGER = 300;
defaultofficeI: INTEGER = 200;
defaultsalaryD: REAL = 10000.0;
//
bossO : CLASSEmployee;
topworkerO : CLASSEmployee;
worker1O : CLASSEmployee;
IMPLEMENTATION
//
{$R *.DFM}
//
// constructor function overload
CONSTRUCTOR CLASSEmployee.Create( nameS: STRING; employeeidI: INTEGER;
officenumberI: INTEGER; salaryD: REAL );
BEGIN
SELF.nameS := nameS;
SELF.employeeidI := employeeidI;
SELF.officenumberI := officenumberI;
SELF.salaryD := salaryD;
END;
// constructor function overload
CONSTRUCTOR CLASSEmployee.Create( nameS: STRING );
BEGIN
SELF.nameS := nameS;
employeeidI := defaultidI;
defaultidI := defaultidI + 1;
officenumberI := defaultofficeI;
defaultofficeI := defaultofficeI + 1;
salaryD := defaultsalaryD;
END;
// procedure overload
PROCEDURE CLASSEmployee.PROCEmployeeShowRecord( xI: INTEGER; yI:
INTEGER );
BEGIN
ShowMessage( 'Name = ' + nameS + #13 +
'Office = ' + IntToStr( officenumberI ) + #13 +
'Employee Id = ' + IntToStr( employeeidI ) + #13 +
'Salary = $' + FloatToStr( salaryD ) );
END;
// procedure overload
PROCEDURE CLASSEmployee.PROCEmployeeShowRecord;
BEGIN
ShowMessage( 'Name = ' + nameS );
ShowMessage( 'Office = ' + IntToStr( officenumberI ) );
ShowMessage( 'Employee Id = ' + IntToStr( employeeidI ) );
ShowMessage( 'Salary = $' + FloatToStr( salaryD ) );
END;
//
BEGIN
//
bossO := CLASSEmployee.Create( 'Suzanne' );
topworkerO := CLASSEmployee.Create( 'Vanessa', 101, 124, 25000.0 );
worker1O := CLASSEmployee.Create( 'Nathalie' );
bossO.PROCEmployeeShowRecord( 1, 1 );
topworkerO.PROCEmployeeShowRecord( 1, 1 );
worker1O.PROCEmployeeShowRecord( 1, 1 );
//
bossO.Free;
topworkerO.Free;
worker1O.Free;
END.
//
// if you run it, you will see:
//
// Name = Suzanne
// Office = 200
// Employee Id = 300
// Salary = $10000
//
// Name = Vanesssa
// Office = 124
// Employee Id = 101
// Salary = $25000
//
// Name = Nathalie
// Office = 201
// Employee Id = 301
// Salary = $10000
//
//
---
---
Internet: see also:
---
Delphi: Class: Create: Simple: How to create a class in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/24789/fid/175
---
C#: How to use the 'this' command in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24776/fid/791
---
C++: How to use the 'this' command in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/16106
---
Java: How to use the 'this' command in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/16104
---
Visual Basic: How to use the 'this' command (='Me') in Visual
Basic.NET?
http://www.faqts.com/knowledge_base/view.phtml/aid/27372/fid/1610
----------------------------------------------------------------------