Two new language features in the next version

Discussion in 'Android Questions' started by Erel, Sep 6, 2012.

  1. Erel

    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:
    Code:
    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:
    Code:
    '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
     
  2. CARTHO

    CARTHO Member Licensed User

    Great work!
    Much appreciated!
     
  3. Roger Garstang

    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:

    Code:
    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:

    Code:
    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:

    Code:
    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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    No, the dim location hasn't changed.
     
  5. flyingbag

    flyingbag Member Licensed User

    :sign0098::sign0060:
     
  6. Roger Garstang

    Roger Garstang Well-Known Member Licensed User

    So, what happens if I do this then:

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

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

    Code:
    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"
     
  7. Informatix

    Informatix Expert Licensed User

  8. salmander

    salmander Active Member Licensed User

    Can't wait to give it a go!
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    The assignment happens in the exact place you wrote it.

    Yes.

    The assignment operator in Basic4android (and other Basic variants I believe) doesn't return a value.

    Consider this valid code:
    Code:
    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
     
  10. Jost aus Soest

    Jost aus Soest Active Member Licensed User

    Good news! :)
     
  11. Ricky D

    Ricky D Active Member Licensed User

    This will be very useful indeed. Looking forward to it.

    Regards, Ricky
     
  12. barx

    barx Well-Known Member Licensed User

    :sign0098:
     
  13. fredo

    fredo Active Member Licensed User

    Great! Makes the code more readable and increases productivity. I just love to work with your product.

    Gesendet von meinem HUAWEI MediaPad mit Tapatalk 2
     
  14. Penko

    Penko Active Member Licensed User

    Looks great, finally no need to use the semicolon to assign value.

    Sent from my HTC Desire using Tapatalk
     
  15. ukimiku

    ukimiku Active Member Licensed User

    So will both the following assignments be allowed?
    Code:
    dim h = "Hello" as string
    and
    Code:
    dim h as string = "Hello"
    ?

    Thanks, I think is is a good new feature!

    Regards,
     
  16. Theera

    Theera Well-Known Member Licensed User

    It mean to be Constant,Yes or No?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
    No. These are regular assignments.
     
  18. JoanRPM

    JoanRPM Active Member Licensed User

    Great! Good job!
     
  19. Penko

    Penko Active Member Licensed User

    What about easy initialization of classes?

    Now:
    Dim a As cls
    a.Initialize(1,2,3)

    Can be:
    Dim a As New cls(1,2,3)


    Sent from my HTC Desire using Tapatalk
     
  20. AscySoft

    AscySoft Active Member Licensed User

    So this is getting better every day. Can't wait.
     
Loading...