AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Zuweisung für Enums überladen

Ein Thema von Codewalker · begonnen am 12. Apr 2012 · letzter Beitrag vom 12. Apr 2012
 
Panthrax

Registriert seit: 18. Feb 2005
286 Beiträge
 
Delphi 2010 Enterprise
 
#6

AW: Zuweisung für Enums überladen

  Alt 12. Apr 2012, 20: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 = 'Einsthen
    Result := seOne
  else
  if Value = 'Zweithen
    Result := seTwo
  else
  if Value = 'Dreithen
    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.
Code:
MyEnum: Zwei
"Es gibt keine schlimmere Lüge als die Wahrheit, die von denen, die sie hören, missverstanden wird."
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:49 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz