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
Public Propertywrites the getter and setter for you.Sub Newis the constructor;Mereaches the instance.- Overriding
ToStringdecides what WriteLine prints.
Keywords and builtins used here
ClassDimDoubleEndFunctionKmMeNameNewOverridesPropertyPublicReturnStringSubToStringTrail
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.
Step 1 of 3 in Classes & interfaces, step 16 of 29 in Language basics.