![]() |
Properites mit Enums zusammenfassen
Hallo wir haben etliche Aufzählungstsypen die dann jeweils zu drei Eigenschaften führen. Beispiel:
Delphi-Quellcode:
Gibt es nun eine Möglichkeit die drei jeweils zu einer Eigenschaft zusammenzu fassen?
type
TBla = (a,b,c); TBlaSet = set of TBla; TBlub = (l,m,o,p); TBlubSet = set of TBlub; ... property Bla: TBla read GetBla write SetBla; property SupportedBlas: TCharactBlaSet read GetSupportedBlas; property AvailableBlas: TCharactBlaSet read GetAvailableBlas; property Blub: TBlub read GetBlub write SetBlub; property SupportedBlubs: TCharactBlubSet read GetSupportedBlubs; property AvailableBlubs: TCharactBlubSet read GetAvailableBlubs; Ich habe es schon mit records und Klassen versucht in Verbindung mit Generics leider jedoch ohne Erfolg. Geht das irgendwie mit Delphi? (Leider closed: ![]() |
AW: Properites mit Enums zusammenfassen
Schau mal hier
![]() unter "Indexbezeichner". Statt 0..3 und Integer kannst du hier auch deine Enumtypen verwenden:
Delphi-Quellcode:
TMyClass = class
private FProps: array[TBla] of Longint; function GetProp(Index: TBla): Longint; procedure SetProp(Index: TBla; Value: Longint); public property PropA: Longint index a read GetProp write SetProp; property PropB: Longint index b read GetProp write SetProp; property PropC: Longint index c read GetProp write SetProp; property Props[Index: TBla]: Longint read GetProp write SetProp; end; |
AW: Properites mit Enums zusammenfassen
Ach so. Sorry, meine Anfrage war nicht vollständig. Die ganzen Eigenschaften sind nicht beieinander sondern in verschiedenen Klassen und/oder deren Basen. Ich möchte nicht TBla und TBlub zusammenfassen sondern das einzelne mit Supported und Available.
Ich stelle mir dann vor je eine Eigenschaft die je die drei zusammenfasst
Delphi-Quellcode:
property Bla: ...
... Bla.Actual := ... if x in Bla.Supported |
AW: Properites mit Enums zusammenfassen
Man könnte auch die Elements selbst gruppieren und mit einem RecordHelper versehen, so in der Art:
Delphi-Quellcode:
TBla = (
// Group Available 1000-1999 a_Available = 1000, b_Available, c_Available // Group Supported 2000-2999 a_Supported = 2000, b_Supported, c_Supported ); TBla_Helper = record helper for TBla function IsAvailable : Boolean; function IsSupported : Boolean; end; TBlaSet = set of TBla; |
AW: Properites mit Enums zusammenfassen
Sowas?
Delphi-Quellcode:
type
TBla = (a, b, c); TBlaSet = set of TBla; TBlub = (l, m, o, p); TBlubSet = set of TBlub; TPropGroup<E, S >= class strict protected FActive: E; function GetActive: E; virtual; abstract; procedure SetActive(Value: E); virtual; abstract; function GetAvailable: S; virtual; abstract; function GetSupported: S; virtual; abstract; public property Active: E read GetActive write SetActive; property Supported: S read GetSupported; property Available: S read GetAvailable; end; TBlaGroup = class(TPropGroup<TBla, TBlaSet>) strict protected function GetActive: TBla; override; procedure SetActive(Value: TBla); override; function GetAvailable: TBlaSet; override; function GetSupported: TBlaSet; override; end; TBlubGroup = class(TPropGroup<TBlub, TBlubSet>) strict protected function GetActive: TBlub; override; procedure SetActive(Value: TBlub); override; function GetAvailable: TBlubSet; override; function GetSupported: TBlubSet; override; end; TForm1 = class(TForm) ButtonTest: TButton; procedure ButtonTestClick(Sender: TObject); strict private FBlaGroup: TBlaGroup; FBlubGroup: TBlubGroup; public property BlaGroup: TBlaGroup read FBlaGroup; property BlubGroup: TBlubGroup read FBlubGroup; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; end;
Delphi-Quellcode:
Du bezahlst halt mit dynamischer Speicherallozierung und virtuellen Funktionsaufrufen.
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent); begin inherited; FBlaGroup := TBlaGroup.Create; FBlubGroup := TBlubGroup.Create; end; destructor TForm1.Destroy; begin FBlaGroup.Free; FBlubGroup.Free; inherited; end; procedure TForm1.ButtonTestClick(Sender: TObject); var bla: TBla; blas: TBlaSet; blub: TBlub; blubs: TBlubSet; begin bla := BlaGroup.Active; blas := BlaGroup.Available; blub := BlubGroup.Active; blubs := BlubGroup.Available; end; { TBlaGroup } function TBlaGroup.GetActive: TBla; begin Result := FActive; end; procedure TBlaGroup.SetActive(Value: TBla); begin FActive := Value; end; function TBlaGroup.GetAvailable: TBlaSet; begin Result := [a, b]; end; function TBlaGroup.GetSupported: TBlaSet; begin Result := [b, c]; end; { TBlubGroup } function TBlubGroup.GetActive: TBlub; begin Result := FActive; end; procedure TBlubGroup.SetActive(Value: TBlub); begin FActive := Value; end; function TBlubGroup.GetAvailable: TBlubSet; begin Result := [l, m]; end; function TBlubGroup.GetSupported: TBlubSet; begin Result := [o, p]; end; |
AW: Properites mit Enums zusammenfassen
Danke:thumb: Ja sowas in der Art. (Das von Rollo62 funktioniert vielleicht auch, aber sicher nicht für diesen Anwendungszweck.
Erster Knackpunkt war Dein Vorschlag den Aufzählungstyp und die Menge separat anzugeben. Da Delphi keine Contrains auf Enums kenn kann das nur so gehen. Eine "ESet = set of E" geht nicht. Mein letzter Stand ist der:
Delphi-Quellcode:
Letzendlich werde ich es aber wohl lassen. Mit dem Package wo das rein sollte ist Delphi eh schon überfordert. Und ich befürchte mit noch mehr Generics wirds noch schlimmer.
type
TBla = (a, b, c); TBlaSet = set of TBla; TBlub = (l, m, o, p); TBlubSet = set of TBlub; TPropGroup<E, S> = class private FActive : E; FSupported: S; FAvailable: S; procedure SetActive(Value: E); function GetActive(): E; function GetSupported(): S; function GetAvailable(): S; public property Active : E read GetActive write SetActive; property Supported: S read GetSupported; property Available: S read GetAvailable; end; TClient = class strict protected FBla: TPropGroup<TBla, TBlaSet>; function GetBla(): TPropGroup<TBla, TBlaSet>; public property Bla: TPropGroup<TBla, TBlaSet> read GetBla; end; |
AW: Properites mit Enums zusammenfassen
Zitat:
Zitat:
Delphi-Quellcode:
TBlaPropGroup = TPropGroup<TBla, TBlaSet>
Was ich so verstanden habe, reduziert das potentiell den Codebloat durch Generics etwas. Und lesbarer ist's vielleicht auch. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:20 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 by Thomas Breitkreuz