typestar

Classes and properties in Visual Basic

Auto-properties, Sub New and a ToString the console respects.

Dim ridge As New Trail("Ridge Loop", 12.4)
Console.WriteLine(ridge)
Console.WriteLine($"half is {ridge.Km / 2} km")

Class Trail
    Public Property Name As String
    Public Property Km As Double

    Public Sub New(name As String, km As Double)
        Me.Name = name
        Me.Km = km
    End Sub

    Public Overrides Function ToString() As String
        Return $"{Name} ({Km:F1} km)"
    End Function
End Class

How it works

  1. Public Property writes the getter and setter for you.
  2. Sub New is the constructor; Me reaches the instance.
  3. Overriding ToString decides what WriteLine prints.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
369
Tokens
83
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 55 seconds.

Type this snippet

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

← Previous Next →