Android Tutorial Variables & Objects in Basic4android

Types

Basic4android type system is derived directly from Java type system.
There are two types of variables: primitives and non-primitives types.
Primitives include the numeric types: Byte, Short, Int, Long, Float and Double.
Primitives also include: Boolean and Char.
List of types with their ranges: http://www.b4x.com/forum/basic4android-getting-started-tutorials/8062-data-type-range.html#post45511

Primitive types are always passed by value to other subs or when assigned to other variables.
For example:
B4X:
Sub S1
 Dim A As Int
 A = 12
 S2(A)
 Log(A) 'Prints 12
End Sub

Sub S2(B As Int)
 B = 45
End Sub
All other types, including arrays of primitives types and strings are categorized as non-primitive types.
When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed.
This means that the data itself isn't duplicated.
It is slightly different than passing by reference as you cannot change the reference of the original variable.

All types can be treated as Objects.
Collections like lists and maps work with Objects and therefore can store any value.
Here is an example of a common mistake, where the developer tries to add several arrays to a list:
B4X:
Dim arr(3) As Int
Dim List1 As List
List1.Initialize
For i = 1 To 5
 arr(0) = i * 2
 arr(1) = i * 2
 arr(2) = i * 2
 List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the list
Log(arr(0)) 'What will be printed here???
You may expect it to print 2. However it will print 10.
We have created a single array and added 5 references of this array to the list.
The values in the single array are the values set in the last iteration.
To fix this we need to create a new array each iteration.
This is done by calling Dim each iteration:
B4X:
Dim arr(3) As Int 'This call is redundant in this case.
Dim List1 As List
List1.Initialize
For i = 1 To 5
 Dim arr(3) As Int
 arr(0) = i * 2
 arr(1) = i * 2
 arr(2) = i * 2
 List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the list
Log(arr(0)) 'Will print 2
Tip: You can use agraham's CollectionsExtra library to copy an array.

Casting


Basic4android casts types automatically as needed. It also converts numbers to strings and vice versa automatically.
In many cases you need to explicitly cast an Object to a specific type.
This can be done by assigning the Object to a variable of the required type.
For example, Sender keyword returns an Object which is the object that raised the event.
The following code changes the color of the pressed button. Note that there are multiple buttons that share the same event sub.
B4X:
Sub Globals
    Dim Btn1, Btn2, Btn3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Btn1.Initialize("Btn")
    Btn2.Initialize("Btn")
    Btn3.Initialize("Btn")
    Activity.AddView(Btn1, 10dip, 10dip, 200dip, 50dip)
    Activity.AddView(Btn2, 10dip, 70dip, 200dip, 50dip)
    Activity.AddView(Btn3, 10dip, 130dip, 200dip, 50dip)
End Sub

Sub Btn_Click
    Dim b As Button
    b = Sender 'Cast the Object to Button
    b.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
End Sub
The above code could also be written more elegantly:
B4X:
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    For i = 0 To 9 'create 10 buttons
        Dim Btn As Button
        Btn.Initialize("Btn")
        Activity.AddView(Btn, 10dip, 10dip + 60dip * i, 200dip, 50dip)
    Next
End Sub

Sub Btn_Click
    Dim b As Button
    b = Sender
    b.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
End Sub
Scope

Variables that are declared in Sub Globals or Sub Process_Globals are global and can be accessed from all subs.
Other variables are local and can only be accessed from the sub that they are declared in.
See the Activity lifecycle tutorial for more information about Globals vs. Process_Globals variables.

Tips

All views types can be treated as Views. This allows you to change the common properties of views easily.
For example, the following code disables all views that are direct children of the activity:
B4X:
For i = 0 To Activity.NumberOfViews - 1
    Dim v As View
    v = Activity.GetView(i)
    v.Enabled = False
Next
If we only want to disable buttons:
B4X:
For i = 0 To Activity.NumberOfViews - 1
    Dim v As View
    v = Activity.GetView(i)
    If v Is Button Then 'check whether it is a Button
        v.Enabled = False
    End If
Next
The Type keyword allows you to create your own type of objects. Custom types behave exactly like other non-primitive types.
 

klaus

Expert
Licensed User
Longtime User
B4X:
Dim edtText(), edtText_val() As EditText
edtText = Arrary As EditText (edittext_1, edittext_2, edittext_3, edittext_4 ..., edittext_10)
edtText_val = Arrary As EditText (edittext_1_val, edittext_2_val, edittext_3_val, edittext_4_val ..., edittext_10_val)
Dim temp1, temp2 As String
For i = 0 To 9
    temp1 = edtText(i).Text
    temp2 = edtText_val(i).Text
Next
 
Top