With blocks in Visual Basic
One object, many touches: the block that drops the repetition.
Dim bill As New Invoice With {.Customer = "Acme Ltd", .Total = 240.0}
With bill
.Total += 36.0
.Paid = .Total < 300
Console.WriteLine($"{.Customer} owes {.Total:F2}")
Console.WriteLine($"settled: {.Paid}")
End With
Class Invoice
Public Property Customer As String
Public Property Total As Double
Public Property Paid As Boolean
End Class
How it works
With billscopes the dots; every bare.Totalmeansbill.Total.- Reads, writes and method calls all shorten the same way.
With {...}on New is the initializer cousin of the block.
Keywords and builtins used here
BooleanClassCustomerDimDoubleEndInvoiceNewPaidPropertyPublicStringTotalWith
The run, in numbers
- Lines
- 14
- Characters to type
- 339
- Tokens
- 72
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 48 seconds.
Step 1 of 3 in VB idioms, step 22 of 29 in Language basics.