typestar

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

  1. The interface declares Area with no body at all.
  2. VB implementations name their contract: Implements IShape.Area.
  3. With {.Radius = 2.0} initializes properties at construction.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Classes & interfaces, step 18 of 29 in Language basics.

← Previous Next →