Skip to main content
Previous sectionNext section

User-defined Basic Code

A typical application consists of some number of user-defined methods and functions which contain the actual application logic. These exist as procedures and code within them is known as “procedure-level code”; code outside of the procedures’ code blocks is known as “script-level code.”

Basic Methods

Within a Caché Basic application, the user-defined business logic (the code for the application) is typically contained within methods defined within classes:

Class MyApp.MyClass [language = basic]
{

Method MyMethod() As %Integer
{
    a = 22
    Return a
}

}
Copy code to clipboard

Note that the primary language for this class is specified (using the language keyword) as “Basic”. This means that all methods will be implemented using Basic. You can override this on a method-by-method basis and it is possible to mix methods using different languages together within the same class.

##super

In a Basic method, you can use the syntax ##super to invoke a method by the same name, inherited from a superclass. (For a general discussion of ##super, see “##super syntax” in “Object-specific ObjectScript Features” in Using Caché Objects.)

If a Basic method uses ##super, and if multiple superclasses define the same method, the system invokes the method in the class that appears first (leftmost) in the inheritance list, regardless of the value of the Inheritance keyword.

Calling a Method

You can call a Basic method from Basic:

x = obj.MyMethod()
Copy code to clipboard

or from ObjectScript:

 Set x = obj.MyMethod()
Copy code to clipboard

Basic Routines and Functions

You can create a Basic routine (similar to an ObjectScript routine but implemented using Basic) and define one or more functions within it:

 'MyRoutine.BAS

Function Add(a,b)
    Return a + b
End Function
Copy code to clipboard

You can call this function from Basic (from outside the MyRoutine.BAS routine):

x = Add@MyRoutine(2,3)
Copy code to clipboard

or from ObjectScript:

 Set x = $$Add^MyRoutine(2,3)
Copy code to clipboard

Calling ObjectScript Functions

Should you need to, it is possible to call ObjectScript functions from Basic.

Note that you can call object methods implemented in ObjectScript from Basic and vice versa using normal object syntax.

ObjectScript Extrinsic Functions

Should you need to call an ObjectScript user-defined (extrinsic) function from Basic, you can do this using a special syntax:

val = Func@Routine(args)
Copy code to clipboard

Where Func is the name of the function and Routine is the name of the routine containing the function.

For example, suppose you have an ObjectScript routine, Math.INT:

  ; Math.INT
Add(a,b) PUBLIC {
    Quit a + b
}
Copy code to clipboard

You can call this function from Caché Basic as follows:

val = Add@Math(a,b)
Copy code to clipboard

This is equivalent to the following ObjectScript code:

 Set val = $$Add^Math(.a,.b)
Copy code to clipboard

Note the following differences between Basic and ObjectScript:

  1. There is no Set command needed in Basic.

  2. You do not need to use $$ in Basic for functions that return values.

  3. Basic uses the “@” character as a delimiter between the function and routine names.

  4. By default, all function arguments are passed by reference in Basic.

    If you want to pass arguments by value in Basic, you need to use the ByVal directive:

    val = Add@Math(ByVal a, ByVal b)
    Copy code to clipboard

ObjectScript Intrinsic Functions

From Basic, you cannot directly call built-in (intrinsic) ObjectScript functions, such as those listed in the “ObjectScript Functions” section of the Caché ObjectScript Reference. You can, however, create a simple ObjectScript function or method that invokes the desired function and call that from Basic.

For example, the following ObjectScript function invokes $ZCOS to get the cosine of a specified value:

  ; MyMathUtils.INT
COS(arg) PUBLIC {
 ; Return the cosine of the argument
    Quit $ZCOS(arg)
}
Copy code to clipboard

You can call this from Caché Basic as follows:

PrintLn "The cosine is " & COS@MyMathUtils(x)
Copy code to clipboard