Xbasic Class Files

Description

In the Web Control Panel, the Xbasic Class category contains user-defined Xbasic classes which can be used in any server-side Xbasic script, including inside Xbasic Function Libraries and Xbasic Modules.

Discussion

Xbasic classes can be saved in external files with the .a5xbclass extensions. Classes stored in these files can be passively loaded by Alpha Anywhere (when the first instance of the class is encountered) or explicitly loaded using the loadClass() function.

The Xbasic filter in the Web Project Control Panel filters project files, displaying external Xbasic classes, Xbasic modules, and Xbasic function libraries that have been defined in the project.

Xbasic classes cannot be placed in subfolders. Class files must be published to the webroot of the web application. Alpha Anywhere looks in the webroot when loading a class file, either explicitly using the loadClass() function or implicitly the first time a variable is DIMmed with the class type.

Creating an Xbasic Class 

Xbasic classes are saved in files using the .a5xbclass extension. It's best practice to save classes in files with a filename that matches the class name. For example, suppose the following class, "helloClass", would be saved in a file named helloClass.a5xbclass:

define class helloClass
    ' English
    function Hello as C()
        Hello="Hello World!"
    end function

    ' Spanish
    function Hola as C()
        Hola="Hola Mundo!"
    end function

    ' Swedish
    function Hej as C()
        Hej="Hej världen!"
    end function
end class

If the helloClass class is defined within a namespace, the namespace is included in the filename. E.g. Hello.helloClass.a5xbclass

define class Hello::helloClass
    ' English
    function Hello as C()
        Hello="Hello World!"
    end function

    ' Spanish
    function Hola as C()
        Hola="Hola Mundo!"
    end function

    ' Swedish
    function Hej as C()
        Hej="Hej världen!"
    end function
end class

Loading an Xbasic Class 

Classes can be loaded explicitly using the loadClass() function. The loadClass() loads the Xbasic .a5xbclass file with a matching filename. For example, to load the helloClass.a5xbclass file which contains the "helloClass" class Xbasic class definition:

dim flag as l
flag = loadClass("helloclass")

dim h as helloClass

It is not strictly necessary to use the loadClass() function to load the class before creating and instance of the class. If the DIM statement fails, Alpha Anywhere will try to load the class itself by searching for the class definition. Alpha Anywhere will automatically load the .a5xbclass file if the filename matches the class name. Searching for the class will add a short delay while Alpha Anywhere tries to resolve the class. Explicitly loading the class using loadClass() eliminates this delay.

Loading a class using loadClass() will also re-load the classes defined in the .a5xbclass file. If changes have been made to a class stored in an .a5xbclass class file, the updated class can be reloaded using loadClass(). Without the loadClass() function, Alpha Anywhere will continue to use the version of the class it initially loaded. The only way to get Alpha Anywhere to reload the class is to either restart Alpha Anywhere or use the loadClass() function.

See Also