Visual Basic-to-LiveCode cheat sheet

Programmers coming into LiveCode from Visual Basic or another Basic language may find the following helpful in figuring out the differences in environments.

In most cases a direct translation of VB to LiveCode is not the best way to code any given problem. As with any programming language, there are many ways to accomplish any task. Direct translation will provide the fastest way to get a project converted, but from there out a rethinking of the code will provide code that executes faster in fewer lines, is more readable and maintainable, and may offer new insights into how to solve a problem.

The following is a starting point for developers looking into converting an existing project or coming to grips with the differences.

Assignments

XTalk languages follow a natural-language approach. In general, statements are of the imperative form in which there is an implied second-person subject of the statement (you) and the verb is the first word of the line: "put value into container", "add 1 to someVariable", etc.

Thus the VB syntax in which some statements have an implied verb ("X=4" instead of "LET X=4") must be translated to LiveCode syntax "put 4 into x".

BASIC LiveCode
lblTemperature.Text = 98.6
put 98.6 into field "lblTemperature"
xyz = xyz + 1
add 1 to xyz
let x = 3.14
put 3.14 into x

Arrays

Arrays: Arrays in LiveCode are associative - the index may be, but does not need to be, numeric in order to access array elements. The VB array syntax arrayName(index) is replaced by arrayName[index]. There is no VB equivalent for a non-numeric array index.

BASIC LiveCode
arrayName(4) = "hello"
put "hello" into arrayName[4]

put "hello" into arrayName["greeting"]

put "bonjour" into arrayName["greeting",tCurrentLanguage]

put "bonjour" into arrayName["greeting"][tCurrentLanguage]

put translationArray["hello"]["French"] into field "localizedGreeting"

User-defined types

While there are no user-defined types in LiveCode, their use can be simulated using arrays:

PrivateType Node
 Row As Integer --row of the actual node
 Col As Integer --column of the actual node
 ParentId As Integer --parent node
 ScoreF As Integer --Score F (total cost)
 ScoreG As Integer --Score G (Cost of the path done)
 ScoreH As Integer --Score H (Estimated cost of the path to do)
 Closed As Boolean --indicates if the node is in the close list
EndType
BASIC LiveCode
Node.Row = 34
Node.Col = 25
Node.Closed = False
local tNode
put 34 into tNode["Row"]
put 25 into tNode["Col"]
put false into tNode["Closed"]
BASIC LiveCode
Dim CurrNode as Node 
Dim TargetNode as Node
TargetNode["Row"] = 34
CurrentNode["Col"] = 25
local tNodes
put 34 into tNodes["TargetNode"]["Row"]
put 25 into tNodes["CurrentNode"]["Col"]
-- pick from the array of arrays
put tNodes["TargetNode"]["Row"] into tRowValue

local tNode
put 34 into Node["Row"]
put Node into tNodes["TargetNode"]
-- get just the individual node array
put tNodes["TargetNode"] into tNode
put tNode["Row"] into tRowValue

Return values

The odd VB syntax of returning a value from a function (procedure) by assigning the value to the name of the procedure is replaced in LiveCode by the more standard return keyword.

BASIC LiveCode
SUB PROCNAME
  PROCNAME = someValue
END SUB
return someValue

Similarities

Similarities: LiveCode, like VB, is not case-specific: somevariable is the same as someVariable, as are SOMEVARIABLE and sOmEvArIAblE. Unless explicitVars is set to true (enable variable checking in the menu) variables do not need to be declared before use. This is similar to VB's OPTION EXPLICIT. In LiveCode, single-word string literals should be, but in general do not have to be (unless explicitVars is true), quoted:

put red into tCurrentColor
put "black" into tCurrentColor
answer hello
answer "hello, sailor"
Click to copy

The following are not necessary in LiveCode:

REDIM
LET
PI is already defined as a constant
Click to copy

Direct mapping from VB to LiveCode

The following VB constants map directly to the following LiveCode keywords:

BASIC LiveCode
vbCRLF
cr or return
vbKeyReturn
cr or return
vbNewLine
Cr
vbOKOnly
"OK"
vbNullString
Empty
vbBlack
"black"
vbWhite
"white"
vbRed
"red"
vbBlue
"blue"
vbGreen
"green"
vbYellow
"yellow"
vbMagenta
"magenta"
vbCyan
"cyan"

more direct mapping:

BASIC LiveCode
' (comment indicator)
"--" or "#" or "//"
DIM X AS INTEGER
local x or global x
PROCEDURE SomeProc
function SomeProc
DO
repeat
WHILE repeat while
WEND end while
LOOP end while
SELECT CASE switch (use break statement between cases)
END SELECT end switch
CASE ELSE default
OPEN fileName as "x"
open file fileName
CLOSE "x" 
close file fileName
OPTION EXPLICIT
explicitVars
TRIM(SomeString)
word 1 to -1 of SomeString

No mapping

The following VB keywords have no mapping:

  • on error
    • use try/catch construct

The following constructs have no direct equivalent:

  • WITH
  • END WITH
  • GOTO label
  • dot notation:
    • instead of "lblMyLabel.Height" use "the height of field "MyLabel"
  • Timer controls
    • possibly use invisible buttons with "send 'Timer' to me in (the interval of me) milliseconds"
  • System and ActiveX controls in external libraries

Screen coordinates

VB screen coordinates are in Twips rather than in pixels. There are usually 15 twips per pixel, although this can vary depending on display characteristics.

Switch statements

SELECT CASE/CASE/CASE ELSE/END SELECT constructs are represented by the switch construct in LiveCode. The biggest difference is that individual case statements need "break" statements to separate them (as in C or java) and to keep the code from falling through into the next case. In VB different case items that should execute the same code are placed on the same line; in LiveCode they are placed on separate lines.

BASIC LiveCode
SELECT CASE tValue
switch tValue
CASE 1 
   routine1
case 1
   routine1
   break
 CASE 2, CASE 3
   routine2 
case2
case3
   routine2
   break
CASE ELSE
   routine3
default
   routine3
END SELECT
end switch

3 Comments

Torsten Holmer

I appreciate the idea of the cheat sheet, but the formatting is bad. Is ist possible to use an table with 2 columns for the sections with the code examples?

Elanor Buchanan

I have tidied the formatting up a bit. Thanks for bringing this to our attention.

Elanor

Torsten

Much better, thank you!!

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.