typestar

Dim and interpolation in Visual Basic

Dim, type inference and the $-string that formats most VB output.

Dim city = "Lisbon"
Dim temperature As Double = 23.7
Dim sunny = True

Console.WriteLine($"weather for {city}")
Console.WriteLine($"currently {temperature}C, sunny: {sunny}")

Const boiling As Integer = 100
Dim headline = city.ToUpper() & " STAYS WARM"
Console.WriteLine(headline)
Console.WriteLine($"{city,-10} {temperature,6:F1}")
Console.WriteLine($"short of boiling by {boiling - CInt(temperature)}")

How it works

  1. Dim declares; the compiler infers String, Double and Boolean here.
  2. $"..." splices any expression between braces into the text.
  3. {city,-10} pads a column; {temperature,6:F1} fixes width and decimals.
  4. CInt converts explicitly — Option Strict expects you to say so.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
404
Tokens
78
Three-star pace
95 tpm

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

Type this snippet

Step 1 of 3 in Variables & strings, step 1 of 29 in Language basics.

Next →