New syntax to talk with windows, controls, databases, for drawing graphics, etc.

Liberty BASIC is not an objected oriented language in the proper sense but it gives some things an 'object' treatment.  In Liberty BASIC an object can be a file, a window, a GUI control, a database accessor, and more.  A new syntax has been added to Liberty BASIC v5.0 which makes for cleaner code when controlling objects.  When a variable is referred to with a # in front of its name, this is known in Liberty BASIC as a handle.  A handle is a special kind of variable that refers to an object.

For example, if you open a window, that window is given a handle and the program 'talks to' the window using its handle.

    open "My program" for window as #myWin

Until Liberty BASIC v5.0 the only way to command the window was by using a sort of streaming protocol like so:

    #myWin "trapclose quit"

This sends a string to the object called #myWin and that object (a window in this case) interprets the meaning of the string.

This approach is still supported in Liberty BASIC v5.0 but we also now have a new way to do this:

    #myWin trapclose("quit")

This sends a message trapclose to #myWin in similar fashion to the first example, and the object interprets the meaning of that message along with the "quit" parameter, which is the name of a SUB.

What are the advantages of this new approach?  When using the legacy streaming approach for sending multiple values, everything is sent as a string and that can make for a messy looking statement.  As an example, if I want to draw a line in a graphics window I must send x and y for each end point of the line, like so:

    'Draw a line from 100, 150 to 200, 250
    #graph "line 100 150 200 250"


That doesn't look so bad, right?  But try to do that with variables and it looks like this:

    'Draw a line using variables, legacy syntax example
    x1 = 100
    y1 = 150
    x2 = 200
    for y2 = 0 to 25
        #graph "line "; x1; " "; y1; " "; x2; " "; y2 * 10
    next y2


This can get pretty messy, but with the new approach it is much cleaner, like so:

    'Draw a line using variable, new syntax example
    x1 = 100
    y1 = 150
    x2 = 200
    for y2 = 0 to 25
        #graph line(x1, y1, x2, y2 * 10)
    next y2


Finally the new approach to talking to objects allows for the returning of a value like a function does.  For example to get the string contents of a textbox in the legacy syntax we would do this:

    'Put the string value of #tbox into varName$
    #tbox "!contents varName$";


With the new syntax the value is returned as with a function, like so:

    'Query the textbox for its value and assign to varName$
    varName$ = #tbox contents$()

But if you don't want to use a variable but just need to include the contents of #tbox in a calculation, you can:

    'Print an uppercase copy of the contents of #tbox
    print upper$(#tbox contents$())


Are there any disadvantages to the new syntax?  Yes, in particular for drawing graphics.  For example:

    #graph "fill blue ; color red; line 10 10 100 100; flush"

This shows how to do several drawing operations in a compact way.  In the new form there is not way to string things together using a semicolon, so it looks like this:

    #graph fill("blue")
    #graph color("red")
    #graph line(10, 10, 100, 100)
    #graph flush()


Of course you can always mix the old syntax with the new syntax when you find it convenient.