Basic4android v2.00 BETA is released

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4android v2.00 BETA is now available. This upgrade is probably the most significant upgrade since v1.00.

Edit: The second beta is now available. See this link: http://www.b4x.com/forum/basic4andr...basic4android-v2-00-second-beta-released.html

The major new feature is support for class modules. With support for classes Basic4android now allows you to write both procedural code and object oriented code.
Object oriented code makes it easier to write and maintain large projects.

B4A v2.00 improvements:

- Classes

- Public and Private access modifiers

- Built-in documentation

- Better handling of tasks in the internal thread pool

- CallSubDelayed keywords - These keywords significantly simplify the interaction between services and activities.

Existing beta testers should soon receive an email with download instructions.
You are more than welcome to join the beta testers. Any user who is eligible for free upgrades (two years for the enterprise version and two months for the standard version) can send an email to [email protected] and join the group.

Issues about the beta version should be posted in the questions forum.
Note that B4A.xml was modified in this version. The highlighting styles section was not modified.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
If we are doing a Project Properties screen we might also want to allow for different resolution icons that get put into their respective folders and the option to switch between that and a single option. I've seen mention in some other Android threads (Not B4A) that talk about themes too (Light, Dark, Translucent), but adding them to my manifest doesn't appear to do anything. Some options for those either at Project or Activity level (wherever they go) would be good too if it is known how to use them and work.
 
Upvote 0

DeerBear

Member
Licensed User
Longtime User
Basic4android v2.00 BETA is now available. This upgrade is probably the most significant upgrade since v1.00.
Existing beta testers should soon receive an email with download instructions.
You are more than welcome to join the beta testers. Any user who is eligible for free upgrades (two years for the enterprise version and two months for the standard version) can send an email to [email protected] and join the group.

Which I did this early morning, no email yet :(

A
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Using a class can I do this...

B4X:
'class Test
Public Id As Int
Public Thing As String

Sub Initialize 
    Id = 0
    Thing = ""
End Sub

Sub Initialize2(id As Int, thing As String 
    Initialize 
    Id = id
    Thing = thing
End Sub

Dim t As Test 
t. Initialize2 (3,  "a thing")

Regards, Ricky
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Can I use an array of objects as a parameter?

If so then I'd test the length of the array to do
1. Initialize an "empty" object
Or 2. Initialize with values
Or 3. Pass a cursor to populate from

What would the signature be?

Initialize(args[])
Select args.Size
Case 0
Fill empty values
Case 1
Dim o As Object
o = args(0)
If o Is Cursor Then
'get the values from the cursor
Else
'try to get values from the array
End If
End Select
End Sub

Regards, Ricky
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Doing that would need 2 lines of code like

t.Initialize
t.Populate(values)

or

t. Initialize
t. PopulateFromCursor(cur)

I'd like to do all 3 in one sub

Regards, Ricky
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
How hard would it be to enhance this model to not require initialize so much? I think we could gain a static method like functionality and much more with just a couple tweaks. We need to make full use of "new" and "as". We have something similar with arrays that I use a lot because I don't like to Dim an array just to pass it to a sub so I use the "array as int(1,2,3,4,5)" type syntax where an array/list parameter is required.

I understand we need the ability to declare an object and have it null like it is now so we can store a reference to an object in it and it doesn't make sense to auto-initialize everything when it may not be used. But I see a lot of syntax like:

dim b as int: b= 3


Something would set b=0 if not set to 3 since Java doesn't do this for us, so when this is converted to Java I'm guessing it is:

int b=0
b=3

Unless the parser is specifically coded to recognize that line and changes it to:

int b=3

Now BASIC is usually a tricky language to do like Java does where we could use:

dim b=3 as int

or

dim b=3, c=4, d=5 as int

Some BASICs have done away with dim which we could do now and use local, public, private and just keeping dim for backwards compatibility. It would be cool if the = format could be used, or perhaps flip the format and do like Java declares (Probably too late in the game for this unless both are allowed)

Along these same lines though it would be easy to declare a class better:

dim myVar as new MyClass

This would then Fully Create myVar like calling:

dim myVar as MyClass
myVar.Initialize

You could call the Initialize that has parameters too like:

dim myVar as new MyClass(param1, param2)

Would be interesting to allow multiple subs called Initialize with different parameters too (Polymorphism...even if only with the Initialize sub would be excellent). In Ricky's example:

B4X:
Sub Initialize 
    Id = 0
    Thing = ""
End Sub

could be:

B4X:
Sub Initialize 
    Initialize2(0, "") ' or Initialize(0, "") if Polymorphic Initialize allowed
End Sub

I don't claim to know all the inner workings, but the addition of the word "new" would do whatever is needed to create the object so no null pointer and calls Class_Globals, but not Initialize yet (At that point in the line). Then whatever is passed after new gets called. Then his alternate Initialize with parameters would work. Initialize2 {Initialize(id As Int, thing As String) if Polymorphic} could even call Initialize then configure other variables on its own.

In some cases you could even skip the new keyword if the sub returns the object:

B4X:
sub CreateButton(text as string) as Button
dim myButton as Button
   myButton.Initialize(text)
   myButton.text = text
   return myButton
end sub

dim ButtonView as CreateButton("OK") ' Button View is all ready to go and no need to Initialize

You could even extend this to views by adding some alternate Initialize methods in the internal classes:

B4X:
' Button Class
Private EventName as string

sub Initialize
   Initialize("Button")
end sub

sub Initialize(EventPrefix as string)
   EventName = EventPrefix
end sub
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Here is my way of doing the initializing how I would like:

B4X:
'Class module Shift
Sub Class_Globals
   Public Id As Int 
   Public ShiftDate As String
   Public CompanyId As Int
   Public CompanyName As String
   Public ShiftTypeId As Int
   Public ShiftType As String
   Public SortSeq As Int
   Public AddingNew As Boolean 
End Sub

'Initializes the object. You can add parameters to this method if needed.
'Accepts a map of values.
'An empty map tells us to Inilialize an "empty" object set to default values
Sub Initialize(mapValues As Map)
   Id = 0
   ShiftDate = "unknown"
   CompanyId = 0
   CompanyName = "unknown"
   ShiftTypeId = 0
   ShiftType = "unknown"
   SortSeq = 0
   Select mapValues.Size
      Case 0
         AddingNew = False
         
      Case 1   'Is it a cursor? If so assume it's holding what we need & is positioned at the record we want
         Dim obj As Object
         obj = mapValues.GetValueAt(0)
         If obj Is Cursor Then
            Dim cur As Cursor 
            cur = obj
            Id = cur.GetInt("Id")
            ShiftDate = cur.GetString("ShiftDate")
            CompanyId = cur.GetInt("CompanyId")
            CompanyName = cur.GetString("CompanyName")
            ShiftTypeId = cur.Getint("ShiftTypeId")
            ShiftType = cur.GetString("ShiftType")
            SortSeq = cur.GetInt("SortSeq")
         End If
         
      Case Else   'get values from mapValues 
         If mapValues.ContainsKey("Id") Then Id = mapValues.Get("Id")
         If mapValues.ContainsKey("ShiftDate") Then Id = mapValues.Get("ShiftDate")
         If mapValues.ContainsKey("CompanyId") Then CompanyId = mapValues.Get("CompanyId")
         If mapValues.ContainsKey("CompanyName") Then CompanyName = mapValues.Get("CompanyNames")
         If mapValues.ContainsKey("ShiftTypeId") Then ShiftTypeId = mapValues.Get("ShiftTypeId")
         If mapValues.ContainsKey("ShiftType") Then ShiftType = mapValues.Get("ShiftType")
         If mapValues.ContainsKey("SortSeq") Then SortSeq = mapValues.Get("SortSeq")
   End Select
End Sub

'Save the contents of the Shift.
'Do a db Insert if AddingNew is True or Update if it is False
'Returns True if it was successful or False if there is an error
Public Sub Save As Boolean
   Dim ret As Boolean : ret = False
   'code to save will go here
   Return ret
End Sub

so calling it like this
B4X:
Dim Shift As Shift
Dim m As Map : m.Initialize
Shift.Initialize(m)  'Create the "empty" object

or

m.Put("cursor", cur)
Shift.Initialize(m)  'populate from a cursor

or

m.Put("Id", db.NextId("Shifts"))
m.Put("ShiftDate", "20120615")
.
.
.
Shift.Initialize(m)

I love this new feature!!

regards, Ricky
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
ERROR if trying to use Try Catch

In a class I can't put any Try Catch blocks

I get a compile error.

What's going on?

this is the compile output

Compiling code. 0.05
Compiling layouts code. 0.02
Generating R file. 0.00
Compiling generated Java code. Error
B4A line: 66
ret = False
javac 1.6.0_30
src\ricky\busdriving02\shift.java:162: cannot find symbol
symbol : variable processBA
location: class ricky.busdriving02.shift
processBA.setLastException(e); //BA.debugLineNum = 66;BA.debugLine="ret = False";

here is the sub code

B4X:
'Class module Shift
Sub Class_Globals
   Public Id As Int 
   Public ShiftDate As String
   Public CompanyId As Int
   Public CompanyName As String
   Public ShiftTypeId As Int
   Public ShiftType As String
   Public SortSeq As Int
   Public AddingNew As Boolean 
End Sub

'Initializes the object. You can add parameters to this method if needed.
'Accepts a map of values.
'An empty map tells us to Inilialize an "empty" object set to default values
Sub Initialize(mapValues As Map)
   Id = 0
   ShiftDate = "unknown"
   CompanyId = 0
   CompanyName = "unknown"
   ShiftTypeId = 0
   ShiftType = "unknown"
   SortSeq = 0
   Select mapValues.Size
      Case 0
          AddingNew = False
         
      Case 1   'Is it a cursor? If so assume it's holding what we need & is positioned at the record we want
         Dim obj As Object
         obj = mapValues.GetValueAt(0)
         If obj Is Cursor Then
            Dim cur As Cursor 
            cur = obj
            Id = cur.GetInt("Id")
            ShiftDate = cur.GetString("ShiftDate")
            CompanyId = cur.GetInt("CompanyId")
            CompanyName = cur.GetString("CompanyName")
            ShiftTypeId = cur.Getint("ShiftTypeId")
            ShiftType = cur.GetString("ShiftType")
            SortSeq = cur.GetInt("SortSeq")
         End If
         
      Case Else   'get values from mapValues 
         If mapValues.ContainsKey("Id") Then Id = mapValues.Get("Id")
         If mapValues.ContainsKey("ShiftDate") Then Id = mapValues.Get("ShiftDate")
         If mapValues.ContainsKey("CompanyId") Then CompanyId = mapValues.Get("CompanyId")
         If mapValues.ContainsKey("CompanyName") Then CompanyName = mapValues.Get("CompanyNames")
         If mapValues.ContainsKey("ShiftTypeId") Then ShiftTypeId = mapValues.Get("ShiftTypeId")
         If mapValues.ContainsKey("ShiftType") Then ShiftType = mapValues.Get("ShiftType")
         If mapValues.ContainsKey("SortSeq") Then SortSeq = mapValues.Get("SortSeq")
   End Select
End Sub

'Save the contents of the Shift.
'Do a db Insert if AddingNew is True or Update if it is False
'Return True if successful otherwise False
Public Sub Save As Boolean
   Dim s As String
   Dim ret As Boolean : ret = True
   
   If AddingNew Then
      Try
         s = "INSERT INTO Shifts (Id, ShiftDate, CompanyId, ShiftTypeId, SortSeq VALUES(?, ?, ?, ?, ?)"
         db.mydb.ExecNonQuery2(s, Array As Object(Id, ShiftDate, CompanyId, ShiftTypeId, SortSeq))
      Catch
         ret = False
      End Try
   Else
      Try
         s = "UPDATE Shifts SET ShiftDate=?, CompanyId=?, ShiftTypeId=?,"
      Catch
         ret = False
      End Try
   End If
   
   Return ret
End Sub

if I take out the Try Catch End Try statements it compiles.

any ideas?

regards, Ricky
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Icefairy that's the right version number for the beta.

It will be 2.xx when it's released (most likely 2.00)

regards, Ricky
 
Upvote 0

BorisC

New Member
Licensed User
Longtime User
Classes! HURRAY, but how about inheritance?

:sign0098: Nice. I simply love OO.

Maybe I'm asking too much, but have you thought about inheritance?
e.g.
CLASS something_to_draw
CLASS circle EXTENDS something_to_draw
CLASS rectangle EXTENDS something_to_draw

Any chance for that, maybe in a later version?
 
Upvote 0
Top