Basic4android v2.20 will include two new language features that will help with writing "cleaner" code:
- Declaration and assignment in the same line:
B4X:
Dim h As String = "Hello"
'The value expression can be located before or after the type.
'This code is also valid:
Dim i1 = 1, i2 = 2, i3 = 3 As Int
- For Each block. A simpler way to iterate over all values in a collection or array:
B4X:
'Numbers is a List or array.
For Each n As Int In Numbers
Sum = Sum + n
Next
For Each v As View In Activity
If v Is Label Then
Dim lbl As Label = v
lbl.TextSize = 20
End If
Next
'Disable several buttons:
For Each b As Button In Array As Button(btn1, btn2, btn3, btn4, btn5)
b.Enabled = False
Next
Awesome. Much needed and helpful additions. Did the new Dim and assign changes by any chance also improve where the variables get Dimmed in code? Currently when you Dim something in an If for example like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Dim onlyInHere As Int
End If
End Sub
The Java Conversion really makes it like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim onlyInHere As Int
If FirstTime Then
onlyInHere= 0
End If
End Sub
So, will this new way move everything to the outer brackets like:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim onlyInHere As Int = 0
If FirstTime Then
End If
End Sub
Or can we have it Dim it within inner blocks now? This would be nice to be able to only Dim variables that are needed and even give us the ability to Dim a variable to be a different type depending on a condition.