B4AThe 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.
ConstantsAt 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 numbersHexadecimal literal numbers should have "0x" prefixes. Octal numbers should have "0".
Data typesB4A 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.
|VB type char||B4A type char||Data type||Example|
ArraysYou 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
OperatorsIn contrast to a plethora of operators that VB6 has, B4A has only a few. Here they are with their precedence:
- And and Or operators are short-circuited.
- The concatenation operator cannot be mixed with the other ones. If it needs to be, then you must use parentheses to disambiguate its precedence.
- The double-equal operator (==) serves the same purpose as the single one (=), that is, compare for equality.
- As there is no exponentiation operator (^), use the Power
KeywordsI'm using "keyword" here in a loose meaning, Array, LastException, and Sender are not keywords.
|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.|
EnumsB4A has three intrinsic enums, although we cannot create any of our own, as there's no Enum keyword.
ObjectsThere'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.
ErrorsTo 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|
- There's no overflow or division by zero errors. Numbers wrap around, and the division by zero yields the type's maximum value.
Re-declaring a variable in the same scope with the same data type is not an error.
That's a consequence of not having a New keyword. If you want to reset the variable, you declare it again.
FinallyHere 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.