AGB  ·  Datenschutz  ·  Impressum  







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

Property in Interface??

Offene Frage von "winx"
Ein Thema von winx · begonnen am 20. Mär 2006 · letzter Beitrag vom 20. Mär 2006
Antwort Antwort
winx

Registriert seit: 14. Jun 2005
265 Beiträge
 
#1

Property in Interface??

  Alt 20. Mär 2006, 09:11
Hallo,

wie kann ich mir in einem Interface eine Property erstellen???

Delphi-Quellcode:
 IPDFImporter = interface(IInterface)
    //property File:TStringList read write; ???
    procedure convert();
  end;
Danke,
winx
  Mit Zitat antworten Zitat
jbg

Registriert seit: 12. Jun 2002
3.481 Beiträge
 
Delphi 10.1 Berlin Professional
 
#2

Re: Property in Interface??

  Alt 20. Mär 2006, 09:13
Du brauchst dazu eine setter bzw. getter Methode im Interface:

Delphi-Quellcode:
type
  ITest = interface
    function GetMyProp: Integer;
    procedure SetMyProp(Value: Integer);
    property MyProp: Integer read GetProp write SetProp;
  end;
  Mit Zitat antworten Zitat
Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#3

Re: Property in Interface??

  Alt 20. Mär 2006, 09:15
So wie Du es auch von Klassen kennst, allerdings musst Du immer Methoden nutzen, da Interfaces keine eigenen Felder haben
Delphi-Quellcode:
  IContentLanguages0400 = interface(IDispatch)
    function Get_Status: EContentLanguageStatus; safecall;
    procedure Set_Status(Value: EContentLanguageStatus); safecall;

    property Status: EContentLanguageStatus read Get_Status write Set_Status;
  end;
......
Daniel W.
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat
winx

Registriert seit: 14. Jun 2005
265 Beiträge
 
#4

Re: Property in Interface??

  Alt 20. Mär 2006, 09:18
Danke
  Mit Zitat antworten Zitat
Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#5

Re: Property in Interface??

  Alt 20. Mär 2006, 09:50
Moin Mieze, moin Andreas

Da ihr zwei euch doch etwas mehr mit Delphis internals auskennt...
Gibt es hier einen Trick um einen Getter/Setter zu deklarieren, der die gleiche Signatur wie die direkte Verwendung eines Feldes in der read/write Clause hat?


Nehmen wir folgende Klasse:

Delphi-Quellcode:
TSample = class
private
  fValue : Integer;
  procedure setValue(value : Integer);
public
  property Value : Integer read fValue write setValue;
end;
Jetzt noch folgendes Interface

Delphi-Quellcode:
ISample = interface
  function getValue : Integer;
  procedure setValue(value : Integer);
  property Value : Integer read getValue write setValue;
end;
Da man properties nicht abstrakt deklarieren kann, muss ich ja hier explizit eine Getter und Setter methode definieren.

Wenn ich jetzt ISample an eine Property vom Type TSample delegieren will, habe ich das Propblem, dass der Getter nicht die richtige Signatur hat.
Hier fehlt ihm der Getter:
Delphi-Quellcode:
TAggregatedSample = class(TInterfacedObject, ISample)
private
  fSample : TSample;
protected
  property Sample : TSample read fSample implements ISample;
end;
Und das hier ist irgendwie ziemlich unschön:
Delphi-Quellcode:
TAggregatedSample = class(TInterfacedObject, ISample)
private
  fSample : TSample;
protected
  function getValue : Integer;
  property Sample : TSample read fSample implements ISample;
end;
...
function TAggregatedSample.getValue : Integer;
begin
  result := fSample.Value;
end;
Also ist es möglich eine Methode zu deklarieren, die die gleiche Signatur wie "read fValue" hat?
Ich hoffe mit den Beispielen war klar, was ich meinte...
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat
shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#6

Re: Property in Interface??

  Alt 20. Mär 2006, 10:22
Das geht mit einer Methodenzuordnungsklausel:
Delphi-Quellcode:
ISample = interface
  function getValue : Integer;
  procedure setValue(value : Integer);
  property Value : Integer read getValue write setValue;
end;

TIrgendwas = class(TInterfacedObject, ISample)
  function InternGetValue: Integer;
  procedure InternSetValue(value:Integer);

  function ISample.getValue=InternGetValue; // Methodenzuordnungsklausel
  procedure ISample.setValue=InternSetValue; // dito
end;
Andreas
  Mit Zitat antworten Zitat
Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#7

Re: Property in Interface??

  Alt 20. Mär 2006, 10:31
Zitat von shmia:
Das geht mit einer Methodenzuordnungsklausel
Erstmal thx für die Antwort.
Resolution clauses sind mir bekannt, aber da bin ich wieder dabei eine Wrapper funktion zu schreiben.
Ich hatte auf einen Weg gehofft wie ich ein Interface direkt an ein Aggregat delegieren kann, OHNE zusätzlichen Code zu schreiben.
Vllt sollte ich mir mal die Exports einen Packages anschauen. Vllt gibt es eine bestimmte Namensconvention für diese direkten Feld -Getter/-Setter.
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat
jbg

Registriert seit: 12. Jun 2002
3.481 Beiträge
 
Delphi 10.1 Berlin Professional
 
#8

Re: Property in Interface??

  Alt 20. Mär 2006, 10:41
Das geht nicht, weil der Compiler für "read fValue" keine getter Methode generiert, sondern beim Lesezugriff darauf, direkt auf das private-Feld fValue zugreift. In den RTTI wird das Feld TPropInfo.GetProc auf $FFxxxxxx gesetzt, wobei xxxxxx der Offset zu Self ist. Also auch von dort keine Change.
  Mit Zitat antworten Zitat
Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#9

Re: Property in Interface??

  Alt 20. Mär 2006, 10:46
Dachte ich mir schon, aber als "unrealistischer Träumer"™ wollte ich mir erst ein explizites "Nö" abholen.
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat
Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#10

Re: Property in Interface??

  Alt 20. Mär 2006, 10:57
Zitat von Elvis:
Dachte ich mir schon, aber als "unrealistischer Träumer"™ wollte ich mir erst ein explizites "Nö" abholen.
Gerne doch: Mich hat es auch schon des öfteren geärgert

......
Daniel W.
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat
Antwort Antwort


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 00:06 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz