AGB  ·  Datenschutz  ·  Impressum  







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

Klasseninstanz zur Laufzeit bestimmen

Ein Thema von Delbor · begonnen am 1. Aug 2015 · letzter Beitrag vom 4. Aug 2015
 
Dejan Vu
(Gast)

n/a Beiträge
 
#2

AW: Klasseninstanz zur Laufzeit bestimmen

  Alt 2. Aug 2015, 06:42
Wieso sind deine Eventhandler eigentlich alle unterschiedlich deklariert? Reicht nicht ein Typ?, z.B.
TAttributEvent = Procedure (Sender : TObject; Attribut : TAttributsClass) of Object; Na ja, wie Du meinst. Wenn das so wäre, dann reicht folgender Code.
Delphi-Quellcode:
Function TMyForm.CreateEvent(Attribut : TAttributsClass) : TAttributEvent;
begin
  if Attribut=FCommentAttri then result := FCSSCommentEvent
  else if Attribut=FPropertyAttri then Result := FCSSPropertyEvent
  else if ...
  ...
  else Raise Exception.Create('Unknown Attribut: '+Attribut.Name);
end;

Procedure TMyForm.FireEvent(Attribut : TAttributsClass);
Var
  Event : TAttributEvent;

begin
  Event := CreateEvent(Attribut);
  CallEventHandler(Event, Attribut);
end;

Procedure TMyForm.CallEventHandler (Event : TAttributEvent; Attribut : TAttributsClass);
Begin
  if Assigned (Event) Then
    Event(Self, Attribut);
end;
Ja, das ist eine ziemlich lange if-else-Folge. Sieht blöd aus, ist aber normal. Da Deine Eventhandler alle individuell deklariert sind, kannst Du das nette 'CallEventHandler' nicht verwenden, sondern musst die 'If Assigned(Event)' Abfrage für jeden Event neu implementieren.

Noch einfacher geht es übrigens mit einem einfachen TNotifyEvent , denn wenn das 'CommentEvent' gefeuert wird, ist ja klar, das mit den CommitAttributen etwas los ist, ergo muss man das Attribut nicht übergeben.
Delphi-Quellcode:
Function TMyForm.CreateEvent(Attribut : TAttributsClass) : TNotifyEvent;
begin
  if Attribut=FCommentAttri then result := FCSSCommentEvent
  else if Attribut=FPropertyAttri then Result := FCSSPropertyEvent
  else if ...
  ...
  else Raise Exception.Create('Unknown Attribut: '+Attribut.Name);
end;

Procedure TMyForm.FireEvent(Attribut : TAttributsClass);
Var
  Event : TNotifyEvent;

begin
  Event := CreateEvent(Attribut);
  CallEventHandler(Event);
end;

Procedure TMyForm.CallEventHandler (Event : TNotifyEvent);
Begin
  if Assigned (Event) Then
    Event(Self);
end;
  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 20:25 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