Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Variables & Objects in Basic4android

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2011, 10:39 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default 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.basic4ppc.com/forum/basic...html#post45511

Primitive types are always passed by value to other subs or when assigned to other variables.
For example:
Code:
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:
Code:
Dim arr(3As 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:
Code:
Dim arr(3As Int 'This call is redundant in this case.
Dim List1 As List
List1.Initialize
For i = 1 To 5
 
Dim arr(3As 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.
Code:
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, 
10dip10dip200dip50dip)
    Activity.AddView(Btn2, 
10dip70dip200dip50dip)
    Activity.AddView(Btn3, 
10dip130dip200dip50dip)
End Sub

Sub Btn_Click
    
Dim b As Button
    b = 
Sender 'Cast the Object to Button
    b.Color = Colors.RGB(Rnd(0255), Rnd(0255), Rnd(0255))
End Sub
The above code could also be written more elegantly:
Code:
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, 
10dip10dip + 60dip * i, 200dip50dip)
    
Next
End Sub

Sub Btn_Click
    
Dim b As Button
    b = 
Sender
    b.Color = 
Colors.RGB(Rnd(0255), Rnd(0255), Rnd(0255))
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:
Code:
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:
Code:
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.
Reply With Quote
  #2 (permalink)  
Old 03-24-2011, 12:50 PM
Newbie
 
Join Date: Feb 2011
Posts: 8
Default

Wow! very nice introduction about vars and objects.

I think more of these basic introductions could be written about other things B4A

i've been a "classic" programmer for years (php,a lot of delphi,...) and programming in Android is very different of the classic windows world.

thanks!
Reply With Quote
  #3 (permalink)  
Old 07-19-2011, 08:55 AM
Lordshiva1948's Avatar
Newbie
 
Join Date: Jul 2011
Location: UK
Posts: 9
Default

Yes I do agree with other person that this vars and objects tutorial is very helpful for those are new to programing
Reply With Quote
  #4 (permalink)  
Old 02-04-2012, 03:15 PM
Newbie
 
Join Date: Feb 2012
Posts: 1
Post

Hi everybody,
first of all sorry for my "maccheronic" English and my less experience.
I'm use to program in VB (not like a pro but like a joke), my first problem was how can I open a new form the main?
for the moment I just take a look on B4A IDE, and I build my first Form with 2 button, one of those (in my idea) open a new form (friends details for example).

In old VB i use to declare the varibles

DIM Pippo as frmPippo
Pippo.Show

Thanks to everyone who want to help me!
Reply With Quote
  #5 (permalink)  
Old 02-04-2012, 04:11 PM
Knows the basics
 
Join Date: Nov 2011
Location: Im Paradies
Posts: 83
Default

You should study the Beginners Guide. There are no Forms in Android, only Activities
__________________
Bad english? Good kharma :-)
Reply With Quote
  #6 (permalink)  
Old 02-23-2012, 08:48 PM
Newbie
 
Join Date: Mar 2011
Posts: 8
Default

I'm trying to pass a "cursor" object by reference to a function, when used within the function generates an initialization error. Is supposed to be initialized before the function call. Can you pass initialized objects to a functions and sub?

Thank you.
Reply With Quote
  #7 (permalink)  
Old 02-24-2012, 05:55 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

There should not be a problem passing an initialized object. Are you sure that the object is already initialized?
Reply With Quote
  #8 (permalink)  
Old 05-04-2012, 06:48 PM
Junior Member
 
Join Date: Mar 2012
Posts: 11
Default

Is there any way to initialize a variable directly at declaration?

For example:
Dim summary As String = "Summary of results"

I tried to declare it as done in Microsoft VB but couldn't get it working, see url
Dim Statement (Visual Basic)

Thanks for the support.
Reply With Quote
  #9 (permalink)  
Old 05-04-2012, 08:45 PM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 4,461
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

No.

I do it that way:
Code:
Dim summary As String   : summary = "Summary of results"
Best regards.
__________________
Klaus
Switzerland

Beginner's Guide / User's Guide
Reply With Quote
  #10 (permalink)  
Old 05-04-2012, 11:30 PM
Junior Member
 
Join Date: Mar 2012
Posts: 11
Default

Thanks for the reply Klaus, this solution is definitely better than nothing.

Code:
Dim summary As String   : summary = "Summary of results"
Just curious though, what does ": summary " exactly do?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Three-level variables? N1c0_ds Basic4ppc Wishlist 1 03-09-2009 05:34 PM
program variables tremara1 Questions (Windows Mobile) 2 10-16-2008 07:52 AM
Help about variables/objects skipper Questions (Windows Mobile) 6 12-19-2007 03:45 PM
Dim-ing variables colin9876 Questions (Windows Mobile) 8 11-17-2007 05:47 PM
Structure variables Erel Code Samples & Tips 0 05-19-2007 10:36 AM


All times are GMT. The time now is 10:35 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0