Operators, Expressions and Control Flow
5.1 Operator Summary
+ - * / \ Mod ^ & (preferred), + = <> < > <= >=, Is, IsNot, TypeOf...Is, TypeOf...IsNot, Like
Not, And, Or, Xor, AndAlso, OrElse (short-circuit)
If(cond, a, b) (ternary), If(x, fallback) (coalesce) AddressOf, GetType, NameOf, Await, New, TypeOf
5.2 Decisions
If balance < 0 Then
status = "Overdrawn"
ElseIf balance = 0 Then
status = "Empty"
Else
status = "OK"
End If
Select Case code
Case 200, 204 : msg = "Success"
Case 300 To 399 : msg = "Redirect"
Case Is >= 500 : msg = "Server error"
Case Else : msg = "Other"
End Select
Select Case True ' condition-based dispatch
Case age < 13 : group = "Child"
Case age < 65 : group = "Adult"
Case Else : group = "Senior"
End Select5.3 Loops
For i = 0 To 10 Step 2 : Console.Write(i) : Next
For i = 10 To 1 Step -1 : Next
For Each item In items : Next
Do While queue.Count > 0 : queue.Dequeue() : Loop
Do : attempts += 1 : Loop Until attempts = 3
While reader.Read() : End While
' Exit For / Exit Do / Exit While / Continue For / Continue Do / Continue While5.4 With Blocks and Object Initializers
With order
.Id = 1
.Customer = "Ana"
.Lines.Add(New OrderLine With {.Sku = "A1", .Qty = 2})
End With
Dim p = New Product With {.Name = "Pen", .Price = 1.5D}
Dim list = New List(Of Integer) From {1, 2, 3}
Dim map = New Dictionary(Of String, Integer) From {{"a", 1}, {"b", 2}}