Reputation: 2520
In my previous question I was suggested to use Interface: How to implement identical methods with 2 and more Classes?
But nothing complies as I have no knowledge on how to use Interfaces.
I guess you can call this code pseudo :)
type
IDoSomething = interface
['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}']
procedure DoSomething1;
procedure WMSize(var Message: TWMSize); message WM_SIZE; // <- Unknown directive: 'message'
end;
TMyCheckBox = class(TCheckBox, IDoSomething) // <- Undeclared identifier: 'DoSomething1'
end;
TMyRadioButton = class(TRadioButton, IDoSomething) // <- Undeclared identifier: 'DoSomething1'
end;
implementation
procedure IDoSomething.DoSomething1; // Identifier redeclared: 'IDoSomething'
begin
// do the same thing for TMyCheckBox and TRadioButton
end;
procedure IDoSomething.WMSize(var Message: TWMSize);
begin
// handle this message the same as with TMyCheckBox and TRadioButton
end;
How do I use the Interface to centralize the code? and How can I share new properties with the help of the Interface?
Upvotes: 2
Views: 625
Reputation: 7575
This is my point:
1) You can't include in the interface definition
procedure WMSize(var Message: TWMSize); message WM_SIZE;
because it is actually a dynamic method:
Remember methods in interface definition cannot be declared as virtual, dynamic, abstract, or override.
2) Including in the interface definition
procedure WMSize(var Message: TWMSize);
is a nonsense since it's not the signature of the method meant to be implemented further.
The interface definition should be then the bare:
type
IDoSomething = interface
['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}']
procedure DoSomething1;
end;
The following class definitions remain unchanged (same for their implementations):
TMyCheckBox = class(TCheckBox, IDoSomething)
procedure DoSomething1;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
TMyRadioButton = class(TRadioButton, IDoSomething)
procedure DoSomething1;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
implementation
procedure TMyCheckBox.DoSomething1;
begin
//
end;
procedure TMyCheckBox.WMSize(var Message: TWMSize);
begin
//
end;
{ TMyRadioButton }
procedure TMyRadioButton.DoSomething1;
begin
//
end;
procedure TMyRadioButton.WMSize(var Message: TWMSize);
begin
//
end;
Upvotes: 2
Reputation: 958
As the name says: Interfaces are the declaration of the interface - not of the implementation. And that is exaclty what tey are for. They give you the possibility to call routines in different classes even if you don't know anything about the class.
And as totaly different classes can implement the same interface, the implementation could be totaly different.
So your code mus look like this:
type
IDoSomething = interface
['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}']
procedure DoSomething1;
procedure WMSize(var Message: TWMSize);
end;
TMyCheckBox = class(TCheckBox, IDoSomething)
public
procedure DoSomething1;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
TMyRadioButton = class(TRadioButton, IDoSomething)
procedure DoSomething1;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
implementation
procedure TMyCheckBox.DoSomething1;
begin
// do the same thing for TMyCheckBox and TRadioButton
end;
procedure TMyCheckBox.WMSize(var Message: TWMSize);
begin
// handle this message the same as with TMyCheckBox and TRadioButton
end;
procedure TMyRadioButton.DoSomething1;
begin
// do the same thing for TMyCheckBox and TRadioButton
end;
procedure TMyRadioButton.WMSize(var Message: TWMSize);
begin
// handle this message the same as with TMyCheckBox and TRadioButton
end;
Upvotes: 1
Reputation: 36654
the message keyword is not allowed in an interface method declaration. Remove message WM_SIZE;
and the code will run fine until the compiler hits the second problem - see below
Interface methods never have their own implementations. If the IDoSomething
interface declares a method DoSomething1
, the compiler will not allow to write an implementation like procedure IDoSomething.DoSomething1;
like in your code - instead, the class which implements the interface has to provide the implementation body.
Upvotes: 3