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
Dimdeclares; the compiler infers String, Double and Boolean here.$"..."splices any expression between braces into the text.{city,-10}pads a column;{temperature,6:F1}fixes width and decimals.CIntconverts explicitly — Option Strict expects you to say so.
Keywords and builtins used here
ConstDimDoubleIntegerTrue
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.
Step 1 of 3 in Variables & strings, step 1 of 29 in Language basics.