Two new language features in the next version

Erel

Administrator
Staff member
Licensed User
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
 

Roger Garstang

Well-Known Member
Licensed User
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.
 

Roger Garstang

Well-Known Member
Licensed User
So, what happens if I do this then:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Dim onlyInHere As Int = 5
   End If
End Sub
Does it go to:

B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim onlyInHere As Int = 5
   If FirstTime Then
      
   End If
End Sub
or

B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim onlyInHere As Int
   If FirstTime Then
      onlyInHere = 5
   End If
End Sub
Is it allowed to set to a variable like:

Dim var1 as Int = 2
Dim var2 as Int = var1

While on the subject of assigning in one line- I had an assign yesterday that gave me an error when to me it should have been valid:

textview1.text = textview2.text = textview3.text = "Text for Views"
 

Erel

Administrator
Staff member
Licensed User
So, what happens if I do this then:
The assignment happens in the exact place you wrote it.

Is it allowed to set to a variable like:

Dim var1 as Int = 2
Dim var2 as Int = var1
Yes.

While on the subject of assigning in one line- I had an assign yesterday that gave me an error when to me it should have been valid:

textview1.text = textview2.text = textview3.text = "Text for Views"
The assignment operator in Basic4android (and other Basic variants I believe) doesn't return a value.

Consider this valid code:
B4X:
Dim a As Boolean, b, c As Int
...
a = b = c
'What is the value of a?
'This is equivalent to:
If b = c Then a = True Else a = False
 

Ricky D

Well-Known Member
Licensed User
This will be very useful indeed. Looking forward to it.

Regards, Ricky
 

barx

Well-Known Member
Licensed User
:sign0098:
 

ukimiku

Active Member
Licensed User
So will both the following assignments be allowed?
B4X:
dim h = "Hello" as string
and
B4X:
dim h as string = "Hello"
?

Thanks, I think is is a good new feature!

Regards,
 
Top