Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

B4A

The other day I got into my head that I should write an Android app.
It's not the first time I wanted to do that.

3.5 years ago I've Googled what I needed to do to set up an environment. (These days I'd use Duck Duck Go.)
I needed Windows Pro, my Home edition would not fit. So, I've spent around US$ 115.00 on the upgrade.
Then, I needed to change some hardware configurations. Scary, but OK, I guess? I'd also need Android Studio and an Android emulator.
After everything was set up, I started fiddling with it and was not amused by how slow the emulator was. Like, really slow.
Then, I had a BSOD not once, but twice. It killed my determination to become an Android developer.

Now I wanted to try once again. I researched one more time and something different came up: B4A (BASIC for Android).
(It's been free only since Feb/2020, that's why I suppose I did not consider using it back then.)
To be frank, its installation was not as straightforward as it should be. I had to separately download an executable and put it in the right folder so B4A would be able to compile without errors.
After that, I started to do basic stuff (no pun intended) real quickly. My project did not take off for unrelated reasons, but I was intrigued by how much B4A is or isn't different from VB6. I'm a sucker for BASIC dialects, after all.

Constants

At its 10.7 version, it has only eight intrinsic constants: True, False, Null, cE, cPI, CRLF, QUOTE, and TAB. No Nothing or Empty.
Note that CRLF is equal to vbLf instead of vbCrLf as one would expect.

Literal numbers

Hexadecimal literal numbers should have "0x" prefixes. Octal numbers should have "0".

Data types

B4A does not have the Variant data type. Like .NET, Object fulfills both the role of VB6's Object and Variant.
In addition to Object, we have available the following data types: Boolean, Byte, Short (16-bit integer), Int (32-bit integer), Long (64-bit integer), Float (that's Single), Double, Char, and String. Be aware that Byte is signed, ranging from -128 to 127, though.
There are no Currency, Decimal, or Date data types. Long is used to represent Dates and DateTime's methods to handle them.

Type chars

VB type char B4A type char Data type Example
^ L 64-bit integer 20L
! F Single 9F
# D Double 17D

Arrays

You declare an array stating how many elements it should have, instead of what is its upper bound. Example: Dim Months(12) As Int
You can't declare what its lower bound is, as it is fixed in zero.
To declare and initialize an array, you can use the following syntax: Dim Months() = Array As Int(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) As Int

Operators

In contrast to a plethora of operators that VB6 has, B4A has only a few. Here they are with their precedence:

Precedence Operator
&
1 (unary) -
Not
2 *
/
Mod
3 +
-
4 <
<=
=
==
>=
>
<>
5 And
Or

Notes:

Keywords

I'm using "keyword" here in a loose meaning, Array, LastException, and Sender are not keywords.

Keyword Remarks
Array
Dim <variable> As <type> [= <expression>] Differently from VB6, Dim A, B, C As Byte states A, B, and C's type.
Const <name> = <expression>
If / Else / Else If / End If Note that there's a space between Else and If. There's no ElseIf.
Select / Case / End Select It is Select <value>, not Select Case <value>.
There's no Case Is or Case <value> To <value>, but there's Case <value1>, <value2>, <etc>.
Do While / Loop, Do Until / Loop There's no Do / Loop, Do / Loop While, or Do / Loop Until.
For <variable> = <expression> To <expression> [Step <expression>] / Next
For Each <variable> As <type> In <collection> / Next
Continue / Exit Exit or Continue is not followed by For or Do.
Try / Catch / LastException / End Try Oddly enough, there's no Finally.
Private, Public, Sub, Return, Me Subs can return values, so there's no Function keyword.
Type Syntax for Type is different: Type TwoInts(Low As Int, High As Int)
If <variable> Is <type> Then There's no TypeOf.
Sender Used the same as .NET's.
Wait Used in asynchronous calls.

Notes:
  • Even though there's no Property keyword, they are available through naming convention: Subs starting with "get" (lower case) are Property Get, and starting with "set" (lower case, too) are Property Set. (There's no difference between Let and Set.)
  • There are no ByVal or ByRef keywords, all arguments are passed by value.
  • Enums

    B4A has three intrinsic enums, although we cannot create any of our own, as there's no Enum keyword.

    Enums
    Colors
    DialogResponse
    KeyCodes

    Objects

    There's no need to New objects. (Actually, the New keyword itself is missing.) You declare an object variable and start using it, that is, unless the class has an Initialize method.
    In this case, you should call it before using the object.

    Errors

    To be able to raise errors, you'll need to import the Threading library and use its ExceptionEx class.

    Compilation directive and constants

    #If / #End If
    B4A
    DEBUG
    RELEASE

    Oddities

    Finally

    Here are B4A intrinsic subs, functions, and some of its classes:

    Subs: CallSub, CallSubDelayed, CancelScheduledService, DoEvents (deprecated), ExitApplication, InputMap, Log, LogColor, MsgBox, ProgressDialogHide, ProgressDialogShow, RndSeed, Sleep, StartActivity, StartService, StartServiceAt, StartServiceAtExact, StopService, ToastMessageShow.

    Functions: Abs, ACos, ACosD, Asc, ASin, ASinD, Atan, AtanD, BytesToString, CallSub, Ceil, CharsToString, Chr, ConfigureHomeWidget, Cos, CosD, CreateMap, DipToCurrent, Floor, GetDeviceLayoutValues, GetType, InputList, InputMultiList, IsBackgroundTaskRunning, IsDevTool, IsNumber, IsPaused, LoadBitmap, LoadBitmapResize, LoadBitmapSample, Logarithm, Max, Min, Not, NumberFormat, PerXToCurrent, PerYToCurrent, Power, Rnd, Round, Sin, SinD, SmartStringFormatter, Sqrt, SubExists, Tan, TanD.

    Classes: Application, Bit, DateTime, Exception, File, JavaObject, List, Map, Random, Regex, ResumableSub, Timer.

    All in all, B4A is a very interesting thin layer over Android's default programming language: Java.

    Next week we'll talk about haters.

    Andrej Biasic
    2021-12-01