Liberty BASIC Help Online

Handle Variables
 
In versions of Liberty BASIC prior to version 4, the manipulation of files and windows was done using statically declared handles for each file, window or GUI control. Now you can create more reusable code because handle variables allow you to pass a handle using a string form. A handle variable looks like the regular handle but it adds a "$" on the end, like a string variable.
 
Regular handle:   #myHandle
Handle variable:  #myHandleVariable$
 
The handle variable maps to the string variable of the same name, which contains the actual handle.  Here is a simple example that fills the variable called "var$" with a control handle, then uses the associated handle variable called "#var$" to send a command to the checkbox.
 
    'create a window with a checkbox and set it
    checkbox #win.red, "Red", [redSet], [redReset], 10, 10, 400, 24
    open "The new handle variable way" for window as #win
 
    'fill a var with the handle of the checkbox:
    var$ = "#win.red"
 
    'now use the associated handle variable to set the checkbox
    #var$ "set"
 
    wait
 
The handle variables are most useful when accessed in FOR/NEXT loops, thus eliminating many lines of code. They are also essential when using subroutines as event handlers, as in the TRAPCLOSE statement. See the examples that follow.
 
The old way
Liberty BASIC 3 doesn't have handle variables.  Here is an LB3 example where you have to define identical code once for each item.
 
    'create a window with a bunch of checkboxes and set them all
    checkbox #win.red, "Red", [redSet], [redReset], 10, 10, 400, 24
    checkbox #win.blue, "Blue", [blueSet], [blueReset], 10, 35, 400, 24
    checkbox #win.green, "Green", [greenSet], [greenReset], 10, 60, 400, 24
    checkbox #win.yellow, "Yellow", [yellowSet], [yellowReset], 10, 85, 400, 24
    checkbox #win.cyan, "Cyan", [cyanSet], [cyanReset], 10, 110, 400, 24
    open "The old handle way" for window as #win
 
    'set each checkbox
    #win.red "set"
    #win.blue "set"
    #win.green "set"
    #win.yellow "set"
    #win.cyan "set"
    wait
 
The new way
In the code below, it is no longer necessary to issue individual "set" commands for each checkbox as it was in the "old way" example above.  The checkbox handles can be expressed as variables and accessed in a FOR/NEXT loop.  The variable "var$" is reset each time though the loop.  The first time through the loop, it has a value of "#win.red", the second time through the loop it has a value of "#win.blue" and so on. To use it in place of a literal handle for a control, it is written with the "#" character in front, so "#var$" is the handle variable associated with the string variable "var$"
 
    'create a window with a bunch of checkboxes and set them all
    checkbox #win.red, "Red", [redSet], [redReset], 10, 10, 400, 24
    checkbox #win.blue, "Blue", [blueSet], [blueReset], 10, 35, 400, 24
    checkbox #win.green, "Green", [greenSet], [greenReset], 10, 60, 400, 24
    checkbox #win.yellow, "Yellow", [yellowSet], [yellowReset], 10, 85, 400, 24
    checkbox #win.cyan, "Cyan", [cyanSet], [cyanReset], 10, 110, 400, 24
    open "The new handle variable way" for window as #win
 
    'set each checkbox
    for x = 1 to 5
      var$ = "#win."+word$("red blue green yellow cyan", x)
      #var$ "set"
    next x
    wait
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/