AGB  ·  Datenschutz  ·  Impressum  







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

TList / Interfaces

Ein Thema von ConnorMcLeod · begonnen am 19. Jan 2023 · letzter Beitrag vom 20. Jan 2023
 
Benutzerbild von ConnorMcLeod
ConnorMcLeod

Registriert seit: 13. Okt 2010
Ort: Bayern
490 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: TList / Interfaces

  Alt 20. Jan 2023, 04:23
Meine Lösung:
Delphi-Quellcode:
  ISimpleInterface = interface
    function GetID: Integer;
    procedure SetID(AValue: Integer);
    property ID: Integer read GetID write SetID;
  end;
  IModule = interface(ISimpleInterface)
    function GetModNo: Integer;
    procedure SetModNo(AValue: Integer);
    property ModNo: Integer read GetModNo write SetModNo;
  end;
die Liste von diesen Dingern:
Delphi-Quellcode:
  TSimpleIntfList = class(TList<ISimpleInterface>)
  public
    function SimpleItemByID(AiID: Integer): ISimpleInterface;
  end;
eine generische Liste von (beinahe) beliebigem Nachfahrtyp:
Delphi-Quellcode:
  TCustomIntfList<T: ISimpleInterface> = class(TSimpleIntfList)
  strict private
    FrGuid: TGUID;
  public
    constructor Create;
  public
    function TypedItemAtIndex(AiIndex: Integer): T;
  end;
  
uses
    System.SysUtils
  , System.TypInfo
  ;
  
constructor TNntCustomIntfList<T>.Create;
begin
  inherited Create;
  FrGuid := GetTypeData(TypeInfo(T))^.Guid;
end;

function TCustomIntfList<T>.TypedItemAtIndex(AiIndex: Integer): T;
var
  LoItem: ISimpleInterface;
begin
  LoItem := Items[AiIndex];
  Supports(LoItem, FrGuid, Result);
end;
Eine konkrete Nachfahrliste:
Delphi-Quellcode:
  TModuleList = class(TCustomIntfList<IModule>)
    function ModuleByModNo(AiModNo: Integer): IModule;
  end;
  
function ModuleByModNo(AiModNo: Integer): IModule;
var
  LoItem : ISimpleInterface;
  LoModule: IModule;
begin
  for LoItem in Self do begin
    if Supports(LoItem, IModule, LoModule) then begin
      if LoModule.ModNo = AiModNo then begin
        Exit(LoModule);
      end;
    end;
  end;
  Result := nil;
end;
Nr.1 Delphi-Tool: [F7]

Geändert von ConnorMcLeod (20. Jan 2023 um 05:30 Uhr)
  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 01:37 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