I know jumping around in code is not considered professional, but since I'm an amateur, is there a way to do that?
VB6 B4A
=== ===
Global Const x=1 Dim x: x = 1
Control - Events Generate Members
Dim b as Boolean:
If b then... If b = True Then... (You must have the "= True".)
a = a + b If b = True Then a = a - 1
Boolean's value cannot be used in a math function in B4A.
This is probably better style anyway.
This file is primarily for reference when converting Visual Basic
source code to B4A.
VB6 B4A
=== ===
controls Views (button, edittext, label, etc.)
In the VB6 code window, the top left drop-down list contains all
the controls you have placed in the current form and the right list
contains all the events for each control. The equivalen in B4A can
be found by clicking on Designer - Tools - Generate Members. Once
you have created Subs in the program coding window, the tab "Modules"
on the right side will list each of the Subs.
Dim Array(n) Dim Array(n+1)
While "n" is the last index number in VB6, it indicates the number
of array elements when used in B4A. For example, to Dim an array
with 32 elements in VB6, you would have Dim A(32) while to convert
this to B4A, you need to change it to Dim A(33).
Dim b as Boolean:
If b then... If b = True Then... (You must have the "= True".)
a = a + b If b = True Then a = a - 1
Boolean's value cannot be used in a math function in B4A.
ReDim Array() Dim Array() -- to clear an array, just Dim it again.
Global Const x=1 Dim x As Int: x=1
Do [Until/While] same
Loop [Until/While] Loop [Until/While not allowed.]
For - Next same
Exit Do/For Exit
If - Then - Else same, except VB's ElseIf is "Else If" in B4A
--- Continue [Skips to Next in For-Next loop]
For i = 1 to 6
If i = 4 Then Continue
...optional code...
ListView1.AddSingleLine(i & ".")
Next
Colors:
L1.BackColor = L1.Color = Colors.Red
vbRed
L1.ForeColor = L1.TextColor = Colors.Black
vbBlack
DoEvents same
Format() NumberFormat & NumberFormat2 [see documentation]
InputBox($) InputList(Items as List, Title, CheckedItem as Int) as Int
Shows list of choices with radio buttons. Returns index.
CheckedItem is the default.
InputMultiList(Items as List, Title) As List
Usere can select multiple items via checkboxes.
Returns list with the indexes of boxes checked.
MsgBox "text" MsgBox("text", "title")
i=MsgBox() MsgBox2(Message, Title, Positive, Cancel, Negative, Icon) as Int
Displays three buttons with text to display for buttons
(Positive, Cancel, Negative)
Icon is displayed near the title and is specified like:
LoadBitmap(File.DirAssets, "[filename].gif")
--- ToastMessageShow(text, b) [where b=True for long duration]
Rnd is < 1 Rnd(min, max) is integer >= min to < max
Round(n) same, or Round2(n, x) where x=number of decimal places
Calling a sub:
SubName x, y SubName(x, y)
Sub SubName() Sub SubName() As Int/String/etc. -- a Global variable cannot be
a parameter.
Function FuncName() Sub FuncName() with Return [value] at the end (still have
End Sub though)
Exit Sub Return
Exit Function Return [value]
A Sub in B4A is more like a Function in VB6 except that a value is not assigned to
the Sub's name.
Instead, a value is sent via the Return command, as shown above.
Select Case [expr] Select [value]
i = Val(string) If IsNumber(string) Then i = string Else i = 0 --
An attempt to use i=string "throws an exception" if the string is
not numbers.
control.SetFocus view.RequestFocus
n / 0 : error n / 0 = 2147483647 -- B4A does not "throw an exception" for
division by 0, but it does return 2147483647 no matter
what the value of "n" is.
t = Timer t = DateTime.Now ' Ticks are number of milliseconds since 1-1-70
TabIndex:
--------
In VB, TabIndex can be set to control the order in which controls get focus
when Tab is pressed. According to Erel, in B4A:
"Android handles the sequence according to their position. You can set
EditText.ForceDone = True in all your EditTexts. Then catch the
EditText_EnterPressed event and explicitly set the focus to the next
view (with EditText.RequestFocus).
Setting Label Transparency:
--------------------------
Properties - Back Style Designer - Drawable - Alpha
Constants:
---------
"" Quote = Chr$(34)
vbCr CRLF = Chr$(13)
vbCrLf none
String "Members":
----------------
VB6 uses a character position pointer starting with 1.
B4A uses a character Index pointer starting with 0.
VB6 B4A
Mid$("abcde", 1, 1) = "a" = letter array index 0
Mid$("abcde", 2, 1) = "b" = letter array index 1
Mid$("abcde", 3, 1) = "c" = letter array index 2
Mid$("abcde", 4, 1) = "d" = letter array index 3
Mid$("abcde", 5, 1) = "e" = letter array index 4
VB6 B4A
=== ===
Mid$(text, n, 1) text.CharAt(n-1)
Mid$(text, n) text.SubString(n-1)
Mid$(text, n, x) [x=length wanted] text.SubString2(n-1, n+x-1) [n+x-1=end position]
Mid$(text, n, x) = text2 text = text.SubString2(0, n-2) & _
text2.SubString2(0, x-1) & _
text.SubString(n-1 + z) where...
z = Min(x, text2.length)
Left$(text, n) [n=num.of chars.] text.SubString2(0, n)
Right$(text, n) text.SubString(text.Length - n + 1)
If a$ = b$... If a.CompareTo(b)...
If Right$(text, n) = text2... If text.EndsWith(text2)...
If Left$(text, n) = text2... If text.StartsWith(text2)...
If Lcase$(text) = Lcase$(text2)... If text.EqualsIgnoreCase(text2)...
x = Len(text) x = text.Length
text = Replace(text, str, str2) text.Replace(str, str2)
Lcase(text) text.ToLowerCase
Ucase(text) text.ToUpperCase
Trim(text) text.Trim
(no LTrim or RTrim in B4A)
Instr(text, string) text.IndexOf(string)
Instr(int, text, string) text.IndexOf2(string, int)
Returns -1 if not found.
Returns char. index, not position.
Starts search at "int".
If Lcase$(x) = Lcase$(y)... If x.EqualsIgnoreCase(y)...
text = Left$(text, n) & s & text.Insert(n, s)
Right$(Text, y)
Asc(s) [where s = a character] same
Error Trapping:
--------------
VB6:
===
Sub SomeSub
On [Local] Error GoTo ErrorTrap
...some code...
Exit Sub
ErrorTrap:
...optional code for error correction...
Resume [optional: "Next" or line label]
End Sub
B4A:
===
Sub SomeSub
Try
...some code...
Catch [only executes if error above]
Log(LastException) [optional]
...optional code for error correction...
End Try
End Sub
WIth B4A, if you get an error caught in the middle of a large subroutine, you can
NOT make a correction and resume within the code you were executing. Only the code
in "Catch" gets executed. That would seem to make Try-Catch-End Try of use mainly
during development.
Try-Catch in place of GoTo:
--------------------------
Try-Catch can be used as a substitute for GoTo [line label] for forward, but not
backward, jumps. It cannot be used to replace GoSub.
Start the code with "Try" and replace the line label with "Catch".
Replace "GoTo [line label]" with code which will create an exception, which causes
a jump to "Catch", such as OpenInput("bad path", "bad filename").
How to sort a List:
------------------
I searched long and hard to find out how to sort a List and couldn't find it.
A message posted asking how to do it was not answered, so maybe NOBODY knows.
Anyway, here's the syntax:
listName.Sort(True)
That's it. Put it on a line by itself.
Events sub signatures:
---------------------
Each "View" ("control", in VB) has certain "events" it supports, such as a Click
event for a button, TextChanged event for an editText View, etc.
In VB6's code windows, you select a control in the left drop-down list and the
event in the right list.
In B4A, you start by typing "Sub viewName" followed by a space and follow the
prompts, pressing Enter after each selection until B4A ends with "EventName"
highlighted. This is where you would type in the name of the Sub.
Sub SomeSub(...)
...some code...
If something Then GoTo SomePlace
...some more code...
SomePlace:
...code continues...
End Sub
Sub SomeSub(...)
Try
...some code...
If something Then OpenInput("badDirName, "badFileName")
...some more code...
Catch
...optionally, some code to execute before continuing...
...optionally Return to exit sub...
End Try
...code continues...
End Sub
Sub SomeSub(...)
...some code...
SomeLabel:
...code to jump back to continues...
If whatever Then GoTo SomeLabel
...code continiues...
I would have done it in both like this:
Best regards.B4X:Sub SomeSub(...) ...some code... If something Then ...some more code... Else ...code continues... End If ...code continues... End Sub
VB6 B4A
Mid$("abcde", 1, 1) = "a" = letter array index 0
Mid$("abcde", 2, 1) = "b" = letter array index 1
Mid$("abcde", 3, 1) = "c" = letter array index 2
Mid$("abcde", 4, 1) = "d" = letter array index 3
Mid$("abcde", 5, 1) = "e" = letter array index 4
VB6:
Mid$(text, n, x) = text2
B4A:
text = text.SubString2(0, n-2) & _
text2.SubString2(0, x-1) & _
text.SubString(n-1 + z) where...
z = Min(x, text2.length)
Example 1: Mid$("abcde", 2, 3) = "jkl" = "ajkle"
n=2, x=3, text="abcde", text2="jkl"
text.SubString2(0, n-2) = "a"
text2.SubString2(0, x-1) = "jk"
z = Min(x, text2.Length) = 3
text.SubString(n-1 + z) = "e"
= "ajkle"
Example 2: Mid$("abcde", 2, 3) = "jk" = "ajkde"
n=2, x=3, text="abcde", text2="jk"
text.SubString2(0, n-2) = "a"
text2.SubString2(0, x-1) = "jk"
z = Min(x, text2.Length) = 2
text.SubString(n-1 + z) = "de"
= "ajkde"
Example 3: Mid$("abcde", 3, 1) = "jk" = "abjde"
n=3, x=1, text="abcde", text2="jk"
text.SubString2(0, n-2) = "ab"
text2.SubString2(0, x-1) = "j"
z = Min(x, text2.Length) = 1
text.SubString(n-1 + z) = "de"
= "abjde"
You should use a List instead of an array. It allows you to add and remove items and it also has other powerful features.1. In vb6, you have a Preserve keyword to be used along with Redim used for increasing the elements in an array without discarding the existing data. What is the B4PPC equivalent?
There are no classes in B4A. You can use Type to declare your own structures.2. I am used to creating classes in vb6.
Let me throw in two cents - you don't want a GoTo.
You always want to find another way.
In my 35+ years of programming, I have often been faced with a choice of spending a lot of time rewriting code to avoid a GoTo, or simply using a GoTo and getting on with the project. I've never experienced any negative impact from using the GoTo. Sometimes practicality has to take the place of what they told you in Programming 101.
I hear you. And, I reach for the practical solution all the time. But, in my 25+ years of experience, I have:
- Never had to use a GoTo
- Suffered greatly from inheriting code of others that Did
... I would never contenance the use of a GOTO for professionally written software...
My teacher hated:
exit for
exit sub
But they are extremely useful. do they work in B4A?