typestar

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

  1. With bill scopes the dots; every bare .Total means bill.Total.
  2. Reads, writes and method calls all shorten the same way.
  3. With {...} on New is the initializer cousin of the block.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in VB idioms, step 22 of 29 in Language basics.

← Previous Next →