Mega Code Archive

 
Categories / Delphi / Variables
 

Abstracterrorproc - defines a proc called when an abstract method is called

var AbstractErrorProc : Pointer; Description The AbstractErrorProc variable defines to Delphi a procedure that it should call when your code erroneously calls an abstract method of a class. Abstract methods are placeholder methods - only to be implemented by derived classes. The parent class is really a skeleton class, and therefore should not be instantiated into an object. delphi will warn you if you do. If you call an abstract method, it will call AbstractErrorProc before terminating with an error. If not found, it will throw EAbstractError exception instead. Notes You should normally avoid calling abstract methods. Related commands Abstract Defines a class method only implemented in subclasses Addr Gives the address of a variable, function or procedure Procedure Defines a subroutine that does not return a value Example code : A polygon class with an abstract method called // Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils; type // Define a base TPolygon class : // This class is a traingle if 3 sides, square if 4 sides ... TPolygon = class private sideCount : Integer; // How many sides? sideLength : Integer; // How long each side? shapeArea : Double; // Area of the polygon protected procedure setArea; Virtual; Abstract; // Cannot code until sides known published property count : Integer read sideCount; property length : Integer read sideLength; property area : Double read shapeArea; constructor Create(sides, length : Integer); end; // Define a square descendent TSquare = class(TPolygon) protected procedure setArea; override; // Override the abstract method end; // Define the form class used by this unit TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions // Create the TPolygon object constructor TPolygon.Create(sides, length : Integer); begin // Save the number and length of the sides sideCount := sides; sideLength := length; // Set the area using the abstract setArea method : // This call will be satisfied only by a subclass setArea; end; // Implement the abstract setArea parent method for the square procedure TSquare.setArea; begin // Calculate and save the area of the square shapeArea := sideLength * sideLength; end; procedure ReportAbstractError; begin // Report the error ShowMessage('ERROR : Abstract method called.'); end; // Main line code procedure TForm1.FormCreate(Sender: TObject); var square : TSquare; polygon : TPolygon; begin // Set up a procedure to catch calls to abstract errors AbstractErrorProc := Addr(ReportAbstractError); // Create a square square := TSquare.Create(4, 10); // Show the area of the square ShowMessageFmt('Square area = %f',[square.area]); // Try to create a polygon (internally calls abstract method) polygon := TPolygon.Create(2, 10); // Meaningless shape! // Show the area of the polygon ShowMessageFmt('Polygon area = %f',[polygon.area]); end; end. Square area = 100.0 ERROR : Abstract method called. The program then terminates with an error dialog: Runtime error 210 at 00452208