Registriert seit: 18. Feb 2005
286 Beiträge
Delphi 2010 Enterprise
|
AW: Zuweisung für Enums überladen
12. Apr 2012, 21:50
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TSomeEnum = (seOne, seTwo, seThree);
TMyEnum = record
strict private
FValue: TSomeEnum;
class function Translate( const Value: TSomeEnum): String; overload; static;
class function Translate( const Value: String): TSomeEnum; overload; static;
public
class operator Implicit( const Value: String): TMyEnum;
class operator Implicit( const Value: TMyEnum): String;
class operator Implicit( const Value: TMyEnum): TSomeEnum;
class operator Implicit( const Value: TSomeEnum): TMyEnum;
end;
{ TMyEnum }
class operator TMyEnum.Implicit( const Value: TMyEnum): String;
begin
Result := Translate(Value.FValue);
end;
class function TMyEnum.Translate( const Value: TSomeEnum): String;
begin
case Value of
seOne:
Result := ' Eins';
seTwo:
Result := ' Zwei';
seThree:
Result := ' Drei';
end;
end;
class operator TMyEnum.Implicit( const Value: String): TMyEnum;
begin
Result.FValue := Translate(Value);
end;
class operator TMyEnum.Implicit( const Value: TSomeEnum): TMyEnum;
begin
Result.FValue := Value;
end;
class operator TMyEnum.Implicit( const Value: TMyEnum): TSomeEnum;
begin
Result := Value.FValue;
end;
class function TMyEnum.Translate( const Value: String): TSomeEnum;
begin
if Value = ' Eins' then
Result := seOne
else
if Value = ' Zwei' then
Result := seTwo
else
if Value = ' Drei' then
Result := seThree
else
raise Exception.Create(' Ungültiger Wert.');
end;
var
MyEnum: TMyEnum;
SomeEnum: TSomeEnum;
begin
try
SomeEnum := seTwo;
MyEnum := SomeEnum;
WriteLn(' MyEnum: ' + MyEnum);
except
on E: Exception do
Writeln(E.ClassName, ' : ', E. Message);
end;
ReadLn;
end.
"Es gibt keine schlimmere Lüge als die Wahrheit, die von denen, die sie hören, missverstanden wird."
|