...das Typenprüfen von Delphi in Procedur-Aufrufen für eigene Typen nutzen?
Autor: Loïs Bégué
{
Delphi prüft die Typen der übergebenen Variablen für entsprechende
var Parameter einer Prozedure / Funtion anhand der geführten Typen.
Dabei wird unterschieden zwischen strenge und schwache Typendeklarationen.
Das kann man ausnutzen, um bestimmte Bedingungen zu kreieren, die
beim Programmieren respektiert sein müssen: Delphi wird u.U. einen
Compiler-Fehler produzieren.
}
{
Delphi checks the type of variables you pass in a procedure or a function
call for declared var parameters.
It makes a difference if you make a strong or weak type declaration.
You can use this casuality to create a design time proofing rule, proofing
the code at design time. Delphi won't compile a code part if it's
"strong checked".
}
type
// Strenge Prüfung wird veranlasst
// Strong checking will be operated
TCheckedIniFileNameType = type
string;
// Schwache Prüfung bei dieser Typ-Deklaration
// Weak checking for this type declaration
TSimpleIniFileNameType = string;
{ This will be strong checked }
procedure CheckedTypeProcedure(var IniFileName: TCheckedIniFileNameType);
begin
ShowMessage(IniFileName);
end;
{ This will be less strong checked }
procedure SimpleTypeProcedure(var IniFileName: TSimpleIniFileNameType);
begin
ShowMessage(IniFileName);
end;
{ Weakest form of checking }
procedure NoTypeProcedure(var IniFileName: string);
begin
ShowMessage(IniFileName);
end;
procedure TForm4.Button1Click(Sender: TObject);
var
StringOnly: string;
SimpleIniFileName: TSimpleIniFileNameType;
CheckedIniFileName: TCheckedIniFileNameType;
begin
StringOnly := 'C:\TEST.TXT';
SimpleIniFileName := 'C:\TEST.TXT';
CheckedIniFileName := 'C:\TEST.TXT';
// This will compile...
CheckedTypeProcedure(CheckedIniFileName);
SimpleTypeProcedure(SimpleIniFileName);
SimpleTypeProcedure(StringOnly);
NoTypeProcedure(StringOnly);
NoTypeProcedure(SimpleIniFileName);
// This will not...
CheckedTypeProcedure(SimpleIniFileName);
CheckedTypeProcedure(StringOnly);
SimpleTypeProcedure(CheckedIniFileName);
NoTypeProcedure(CheckedIniFileName);
end;
printed from
www.swissdelphicenter.ch
developers knowledge base