![]() |
Problem mit Enumeration
Hallo
Ich habe folgendes Problem und stehe irgendwie auf dem Schlauch...
Delphi-Quellcode:
Da ich derzeit einen funktionerenden Code etwas verschönern will, möchte ich die Version mit Enum anwenden.
type
THanID = (StaPre = 0, StaEmp, StaUns, SpaSte, StaBadSte, StaSheCri, StaSheExp, StaFreRel, StaOnHold); type TForm00 = class(TForm) Button1: TButton; procedure FormShow(Sender: TObject); function Get_a_Bit(const aValue: Cardinal; const Bit: Byte): Boolean; private { Private-Deklarationen } public { Public-Deklarationen } end; var Form00: TForm00; implementation {$R *.dfm} procedure TForm00.FormShow(Sender: TObject); begin Button1.Enabled := Get_a_Bit(Random(255), 1);// funktioniert ist aber in einem umfangreichen Code schwer zu lesen Button1.Enabled := Get_a_Bit(Random(255), StaEmp); // <--- Enum funktioniert nicht end; // get if a particular bit is 1 function TForm00.Get_a_Bit(const aValue: Cardinal; const Bit: Byte): Boolean; begin Result := (aValue and (1 shl Bit)) <> 0; end; Vielleicht hat jemand einen Tipp für mich. Grüße |
AW: Problem mit Enumeration
Weil THanID nunmal kein Integer oder Byte ist, musst du dein Enum nach Integer casten, entweder mit
Delphi-Quellcode:
,
Integer(StaEmp)
Delphi-Quellcode:
oder
Byte(StaEmp)
Delphi-Quellcode:
.
Ord(StaEmp)
MfG Dalai |
AW: Problem mit Enumeration
Zitat:
Grüße |
AW: Problem mit Enumeration
Wobei Ord(...) sauberer ist, da es auch mit großen typen funktioniert und die je nach größe den entsprechenden Datentyp zurückliefert.
|
AW: Problem mit Enumeration
Dir ist klar das Random(255) nur Zahlen im Bereich 0..254 erzeugt?
Delphi-Quellcode:
function Get_a_Bit(const AValue: Cardinal; const Bit: Byte): Boolean; overload;
function Get_a_Bit(const AValue: Cardinal; const Bit: THanID): Boolean; overload; implementation function Get_a_Bit(const AValue: Cardinal; const Bit: Byte): Boolean; begin Result := (AValue and (1 shl Bit)) <> 0; end; function Get_a_Bit(const AValue: Cardinal; const Bit: THanID): Boolean; begin Result := Get_a_Bit(AValue, Ord(Bit)); end; |
AW: Problem mit Enumeration
@Blup
Danke für den Hinweis, ist aber nicht relevant da das "Random(x)" aus einer DB eingelesen wird und im Code dann die Bedeutung der einzelnen Bits erfasst wird. -> war nur eine beispielhafte Darstellung meines Problems. In der Application siehts dann so aus:
Delphi-Quellcode:
Grüße/// ---------------------------------------------------------------------------- /// Present /// ---------------------------------------------------------------------------- if (Get_a_Bit(StaPar.FDTable.FieldByName(FldStaHanID).AsInteger, Byte(StaPre)) = True) then begin Bmp.Canvas.Pen.Width := 1; if (StaPar.FDTable.FieldByName(FldStaHigReaLin_5b).AsBoolean = False) then begin Bmp.Canvas.Pen.Color := clWhite; Bmp.Canvas.Pen.Style := psSolid; end else begin Bmp.Canvas.Pen.Color := ContrastColor(ColSty.BackColor); Bmp.Canvas.Pen.Style := psDot; end; Schlingel |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:23 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