-4

I can do this in other languages that I use. For instance I can do this in PHP when needed for web app creation, but here is what I want to do and can't find a solution:

I want to define an interface say:

unit myBigUnit;

interface

 uses someUnits;

 type
   TsampleType = class
     someVar: Integer;
     someOtherVar: Integer;
     someObj: TneatObj;
     procedure foo;
     function bar : String;
     procedure foobar(a: boolean);

All this in one file. Now I want two files that implement this interface or at least know about it. In php I can just say

class blah implements thisInterface

but I can't find an equivalent in Delphi. What I am trying to do is implement this in one unit while in another one I just want it to know about these functions/procedures/et al so I can then call them from there. I couldn't care less about how it is implemented. I thought this was the whole point of having interfaces and separating them from implementors?

How do I do this in Delphi?

Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28
  • 7
    Have you considered reading the Delphi documentation about interfaces? Delphi's had them since the late '90s, so I can only assume that you just haven't looked very hard. – Rob Kennedy Jul 28 '15 at 20:18
  • I fully agree with Rob. Note that your class declaration does not declare an interface, it declares, as the name says, a class. Classes IMPLEMENT interfaces, they are not interfaces. Interface declarations use the - surprise, surprise - interface keyword. – Rudy Velthuis Jul 28 '15 at 21:22
  • "Delphi Interfaces" didn't give you any search results? – Jerry Dodge Jul 28 '15 at 21:33
  • 1
    Interfaces were introduced in D3. They do the same thing in Delphi as they do in Java, C#, and other languages. I'm not sure if they exist exactly the same in PHP or not. But the "interface" you're focusing on here is the interface section of a unit -- there's also an "implementation" section. Both are required in every Delphi unit. – David Schwartz Jul 29 '15 at 01:15
  • @DavidSchwartz "Both are required in every Delphi unit" is not technically true - and I'm pretty sure that is not what OP is talking about, if you take a look at the answers below. – Jerry Dodge Jul 29 '15 at 03:07
  • It is unfortunate that pascal uses the `interface` keyword twice to mean two completely different things. – J... Jul 29 '15 at 09:20
  • My confusion was why there was the keyword "interface" prior to declaring the class. I wasn't looking in the correct place in the documentation because I am not use to declaring interfaces this way. It has been years since I have used delphi and my understanding how OOP design goes together is vastly improved since the last time I was using it. – Rabbit Guy Jul 29 '15 at 20:59
  • @JerryDodge you're right. I caught that then forgot to edit my comment. I Was going to delete that last sentence, as it adds nothing anyway. – David Schwartz Jul 29 '15 at 21:42

2 Answers2

5

You need to use an actual interface, eg:

 type
   IsampleType = interface
     procedure foo;
     function bar : String;
     procedure foobar(a: boolean);
   end;

An interface can only have methods and properties, not variables.

You can then implement the interface in your classes as needed, eg:

type
  TMyClass = class(TInterfacedObject, IsampleType)
  public
    someVar: Integer;
    someOtherVar: Integer;
    someObj: TneatObj;
    procedure foo;
    function bar : String;
    procedure foobar(a: boolean);
  end;

var
  Sample: IsampleType;
begin
  Sample := TMyClass.Create;
  // use Sample as needed...
end;

Delphi interfaces are reference counted. TInterfacedObject handles the reference counting for you. It automatically frees the object when its reference count falls to 0.

You can find more details in Delphi's documentation:

Object Interfaces Index

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for explaining this more clearly for me. J.. above hit the nail on the head as to the fact that Pascal uses the keyword twice for two different meanings. – Rabbit Guy Jul 29 '15 at 21:00
3

Then you should use an Interface:

...

 type
   IsampleType = Interface
   ..... 

an implement this in your classes:

type
  TIntfType = class(TInterfacedObject, ISampleType)
  ....

and the details you will find with F1 in Delphi...

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94