![]() |
Re: Anzahl Elemente in "set of"
das mit dem in klappt nur, wenn man über eine Konstante geht und die will auch aktuell gehalten werden.
sowas wäre möglich, aber auch nur, wenn die Elemente durchgängig belegt sind und zwischendurch Keine fehlen.
Delphi-Quellcode:
Count := Ord(High(TFontStyle)) - Ord(Low(TFontStyle)) + 1;
|
Re: Anzahl Elemente in "set of"
noch eine Variante :wink:
Delphi-Quellcode:
alex
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline); TFontStyles = set of TFontStyle; function CountStyles(const AStyles: TFontStyles): integer; var f: TFontStyle; begin Result := 0; for f := low(TFontStyle) to high(TFontStyle) do if f in AStyles then inc(Result); end; |
Re: Anzahl Elemente in "set of"
@axel: dafür brauchst du aber auch eine "aktuelle" Variable/Konstante.
deine Funktion + diese Definition (ähnliches gilt auch für die anderen Zählfunktionen) würde hier 8 liefern und nicht 4.
Delphi-Quellcode:
type
TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7); TFontStyles = set of TFontStyle; x := CountStyles([TFontStyle(0)..TFontStyle(7)]); // oder x := CountStyles([fsBold..fsUnderline]); |
Re: Anzahl Elemente in "set of"
evtl. ist RTTI Dein Freund:
Delphi-Quellcode:
Der Aufruf dann:
function CountElements(ATypeAddress: pointer): integer;
var lTypeData: PTypeData; begin lTypeData:= GetTypeData(ATypeAddress); Result := lTypeData^.MaxValue; end;
Delphi-Quellcode:
myNumerousElements:= CountElements(TypInfo(TFontStyle));
|
Re: Anzahl Elemente in "set of"
Zitat:
Das funktioniert selbst bei Sets mit 256 Elementen :) (zumindest unter Lazarus, Delphi kann ich grad nicht testen). Wäre das nicht was für die Code-Library? |
Re: Anzahl Elemente in "set of"
Zitat:
werd ich wohl nochmal nachdenken müssen.:gruebel: alex |
Re: Anzahl Elemente in "set of"
Zitat:
Delphi-Quellcode:
// copy & paste =)
type TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7); |
Re: Anzahl Elemente in "set of"
:gruebel: Zumindes <> 0 geht so...
Delphi-Quellcode:
Zumindest in D5 sind die Bits je nach Listen-Element gesetzt.
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline); TFontStyles = set of TFontStyle; procedure Test(const Styles: TFontStyles); var anzahlAttribute: Integer; begin anzahlAttribute := Byte(Styles); // <--- Tata case anzahlAttribute of 0: ShowMessage('STYLES ist leer'); 1: ShowMessage('STYLES hat ein Attribut'); else ShowMessage(Format('STYLES hat %d Attribute', [anzahlAttribute])); end; end; |
Re: Anzahl Elemente in "set of"
Zitat:
Delphi-Quellcode:
Tata = Compilerfehler
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline=15); TFontStyles = set of TFontStyle; procedure Test(const Styles: TFontStyles); var anzahlAttribute: Integer; begin anzahlAttribute := Byte(Styles); // <--- Tata |
Re: Anzahl Elemente in "set of"
Propier mal die anderen Integertypen.
In D5 kann ich bei Sets bis 8 Elemente so die Bits direkt abfragen. €: Bei mehr als 8 Elementen muß ich ein Word casten. Also ist an der Adresse ein Bit-Muster abgelegt. Da müsste sich doch mit einem Pointer was machen lassen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:34 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