This may not be new to some of you, but even though I use class interfaces quite a lot, I didn’t know about this feature. What you can do is tell a class’s method which method of an interface it actually implements. Here’s a simplyfied example:
In a project I have two classes
Class Person
Sub Walk(fromA As Location, toB As Location)
End Sub
End Class
Class Car
Sub Drive(fromA As Location, toB As Location)
End Sub
End Class
Up until now both classes were used in totally different parts of my program, there was not “touch point” between them. Today I decided it would be nice, if I could move all persons and cars in one go. So I created a class interface
Class Interface CanMove
Sub Move(fromA As Location, toB As Location)
End Sub
End Class Interface
Until today I would have either re-named Person.Walk
and Car.Drive
and all calls to them to Person.Move
and Car.Move
respectively. Or, more likely, I would’ve added a Move
method to both classes, that then would call the existing Walk
or Drive
methods.
But there’s a better way. In the Navigator right-click on the definition of Person.Walk
and select Show Implements
from the context menu. This will add a new field Implements
in the inspector. Enter CanMove.Move
into that field. Do the same for Car.Drive
.
Now a Person
can still Walk
and a Car
can still Drive
, but you can also do this:
dim joe as new Person
dim tim as new Person
dim ferrari as new Car
dim porsche as new Car
dim movables() As CanMove
movables.Append(joe)
movables.Append(tim)
movables.Append(ferrari)
movables.Append(porsche)
for each movable As CanMove in movables
movable.Move(San_Francisco, Cupertino)
next movable
If movable
is a person, Person.Walk
will be called, and if it is a car, Car.Drive
. Neat.
I only wish there was some kind of indicator that a method implements an interface method in the Navigator. Either some badge on the method’s icon, like it is done if a method has an attribute attached to it, or in the method’s name, e.g. Walk -> CanMove.Move
(which I’d prefer).