Unit 3-MAD
Unit 3-MAD
UNIT 3
Process and Activity life cycle
Variables and Objects:
A variable is a symbolic name given to some known or unknown quantity or information, for the
purpose of allowing the name to be used independently of the information it represents. A
variable name in computer source code usually associated with a data storage location and
thus also its contents, and these may change during the course of program execution (source
Wikipedia). 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
Variable Types
Rasure B. BPage
From Google
Mobile Application Development
Other types:
Boolean True or False.
Practically it is saved as a byte with the value of 1 or 0.
String
Strings are made from an array of bytes that end with a null byte (byte with the value of 0).
Object
Objects can hold other types of values.
Primitive types are always passed by value to other subs or when assigned to other variables.
For example:
Sub S1
Private A As Int
A = 12 The variable A = 12
S2(A) It's passed by value to routine
S2 Log(A) ' Prints 12 Variable A still equals 12, even though B was changed in routine
S2.
End Sub
All other types, including arrays of primitive 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
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 Private each iteration:
Names of variables
It is up to you to give any name to a variable, except reserved words.
A variable name must begin with a letter and must be composed by the following
characters A-Z, a-z, 0-9, and underscore "_", no spaces, no brackets etc.
Variable names are case insensitive, that means that Index and index refer to the same
variable.
Example:
Interest = Capital * Rate / 100 is meaningful
n1 = n2 * n3 / 100 not meaningful
it is useful to add to the name a three character prefix that defines its type.
Examples:
lblCapital lbl > Label Capital > purpose
edtInterest edt > EditText Interest > purpose
btnNext btn > Button Next > purpose
Declaring variables
Simple variables
Variables are declared with the Private or the Public keyword followed by the variable name
and the As keyword and followed by the variable type.
Examples:
Rasure B. BPage
From Google
Mobile Application Development
The names of the variables separated by commas and followed by the type declaration.
Private i = 0, j = 2, k = 5 As Int
Private txt = "test" As String, value = 1.05 As Double, flag = False As Boolean
For example, if we want to change the text in a Label view in the code, like
lblCapital.Text = "1200",
we need to reference this Label view by its name lblCapital, this is done with the Private
declaration.
If we never make any reference to this Label view anywhere in the code no declaration is
needed.
Using an event routine for that view doesn't need a declaration either.
To allocate a value to a variable write its name followed by the equal sign and followed by
the value, like:
Capital = 1200
LastName = "SMITH"
Note that for Capital we wrote just 1200 because Capital is a number.
But for LastName we wrote "SMITH" because LastName is a string.
Strings must always be written between double quotes.
Array variables
Arrays are collections of data or objects that can be selected by indices. Arrays can have
multiple dimensions.
The declaration contains the Private or the Public keyword followed by the variable name
LastName, the number of items between brackets (50), the keyword As and the variable type
String
.
Rasure B. BPage
From Google
Mobile Application Development
Examples:
Public LastName(50) As String One dimension array of strings, total number of items 50.
Public Matrix(3, 3) As Double Two dimensions array of Doubles, total number of items 9.
Public Data(3, 5, 10) As Int Three dimensions array of integers, total number of items 150.
The first index of each dimension in an array is 0.
LastName(0), Matrix(0,0), Data(0,0,0)
The last index is equal to the number of items in each dimension minus 1.
LastName(49), Matrix(2,2), Data(2,4,9)
Public LastName(10) As String
Public FirstName(10) As String
Public Address(10) As String
Public City(10) As String
or
This example shows how to access all items in a three dimensional array.
Public Data(3, 5, 10) As Int
For i = 0 To 2
For j = 0 To 4
For k = 0 To 9
Data(i, j, k) = ...
Next
Next
Next
We declare the variable Public NbPers = 10 As Int and set its value to 10.
Then we declare the arrays with this variable instead of the number 10 as before.
The big advantage is if at some point we need to change the number of items, we change
only ONE value.
For i = 0 To NbX - 1
For j = 0 To NbY - 1
Rasure B. BPage
From Google
Mobile Application Development
For k = 0 To NbZ - 1
Data(i, j, k) = ...
Next
Next
Next
Filling an array with the Array keyword :
Public Name() As String
Name = Array As String("Miller", "Smith", "Johnson", "Jordan")
Sub Globals
Private Buttons(6) As Button
End Sub
Sub Buttons_Click
Private btn As Button
btn = Sender
Log("Button " & btn.Tag & " clicked")
End Sub
Type variables
Type cannot be private. Once declared it is available everywhere (similar to Class
modules).
The best place to declare them is in the Process_Globals routine in the Main module.
Let us reuse the example with the data of a person.
Instead of declaring each parameter separately, we can define a personal type variable with
the Type keyword:
The new personal type is Person , then we declare either single variables or arrays of this
personal type.
CurrentUser.FirstName
CurrentUser.LastName
User(1).LastName
User(1).FirstName
Casting
B4X 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 references 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.
Sub Globals
Private Btn1, Btn2, Btn3 As Button
End Sub
Sub Btn_Click
Private btn As Button
btn = Sender ' Cast the Object to Button
btn.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
End Sub
End Sub
Sub Activity_Create(FirstTime As Boolean)
Rasure B. BPage
From Google
Mobile Application Development
Private i As Int
For i = 0 To 9 ' create 10 Buttons
Private Btn As Button
Btn.Initialize("Btn")
Activity.AddView(Btn, 10dip, 10dip + 60dip * i, 200dip, 50dip)
Next
End Sub
Sub Btn_Click
Private btn As Button
btn = Sender ' Cast the Object to Button
btn.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
End Sub
Scope
Process variables
These variables live as long as the process lives.
You should declare these variables inside Sub Process_Globals.
This sub is called once when the process starts (this is true for all modules, not just the
main module).
These variables are the only "public" variables.
Which means that they can be accessed from other modules as well.
However, in B4A, not all types of objects can be declared as process variables.
Example:
Variable defined in a module with the name : MyModule
Sub Process_Globals
Public MyVar As String
End Sub
Rasure B. BPage
From Google
Mobile Application Development
Activity variables
These variables are contained by the activity.
You should declare these variables inside Sub Globals.
These variables are "private" and can only be accessed from the current activity module.
All object types can be declared as activity variables.
Every time the activity is created, Sub Globals is called (before Activity_Create).
These variables exist as long as the activity exists
Local variables
Variables declared in a subroutine are local to this subroutine.
They are "private" and can only be accessed from within the subroutine where they were
declared.
All objects types can be declared as local variables.
At each call of the subroutine the local variables are initialized to their default value or to
any other value you have defined in the code and are 'destroyed' when the subroutine is
exited.
Tips
A view / node can be assigned to a variable so you can easily change the common
properties of the view.
For example, the following code disables all views that are direct children of a Panel / Pane:
For i = 0 To MyPanel.NumberOfViews - 1
Private v As View
v = MyPanel.GetView(i)
v.Enabled = False
Next
If we only want to disable buttons:
For i = 0 To MyPanel.NumberOfViews - 1
Rasure B. BPage
From Google
Mobile Application Development
Private v As View
v = MyPanel.GetView(i)
If v Is Button Then ' check whether it is a Button
v.Enabled = False
End If
Next
***************************END**************************
Rasure B. BPage
From Google