Interfaces and Implements in Visual Basic
A contract, and the Implements clause that names its promise.
Dim round As IShape = New Circle With {.Radius = 2.0}
Console.WriteLine($"area: {round.Area():F2}")
Interface IShape
Function Area() As Double
End Interface
Class Circle
Implements IShape
Public Property Radius As Double
Public Function Area() As Double Implements IShape.Area
Return Math.PI * Radius * Radius
End Function
End Class
How it works
- The interface declares
Areawith no body at all. - VB implementations name their contract:
Implements IShape.Area. With {.Radius = 2.0}initializes properties at construction.
Keywords and builtins used here
AreaCircleClassDimDoubleEndFunctionImplementsInterfaceNewPropertyPublicRadiusReturnWith
The run, in numbers
- Lines
- 16
- Characters to type
- 336
- Tokens
- 65
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 46 seconds.
Step 3 of 3 in Classes & interfaces, step 18 of 29 in Language basics.