Using Caché Basic
Frequently Asked Questions About Caché Basic
[Back] 
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Search:    

This Question and Answer Set includes the following topics:
  • General
  • Programming
  • Support

  • General
    Question:
    Does Caché Basic work only on Windows platforms?
    Answer:
    No, Caché Basic is completely platform-independent and works on any platform on which Caché runs. Caché Basic is built into the Caché kernel in the same way as Caché ObjectScript and is not dependent on any external script engine, such as the Microsoft Scripting Host.
    Question:
    Is Caché Basic slower than Caché ObjectScript?
    Answer:
    No, Caché Basic source is compiled into the same object code as Caché ObjectScript, so the performance of Caché Basic is the same (with minor variations) as that of Caché ObjectScript.
    Question:
    On what breed of Basic is Caché Basic based?
    Answer:
    The syntax of Caché Basic is based on Microsoft VBScript. However, since Caché Basic is a server-side scripting language, tightly integrated with the database, there are a number of differences between these two implementations. For instance, there are no MsgBox or InputBox functions in Caché Basic, but there are a number of enhancements; for example, the ability to work directly with Caché-specific data types, such as globals, lists, and so on.
    Question:
    Does the introduction of Caché Basic mean that InterSystems is planning to drop support of Caché ObjectScript?
    Answer:
    No, the main goal of Caché Basic is to ease the Caché learning curve for developers already familiar with Basic implementations such as Microsoft Visual Basic, not to replace Caché ObjectScript. Both Caché ObjectScript and Caché Basic will be supported and coexist indefinitely.
    Question:
    Are there any other goals beyond “easing the learning curve?”
    Answer:
    Yes, with the introduction of Caché Basic, companies using Caché will gain a number of benefits, including:
    Programming
    Question:
    Is there a “Caché Basic shell”?
    Answer:
    No, but nothing prevents you from implementing your own. Contact an InterSystems representative for an example of a Caché Basic shell.
    Question:
    How do I work with globals in Caché Basic?
    Answer:
    You can directly reference globals using the following syntax:
    Caché Basic Syntax Elements
    Syntax element Description
    ^Global(index)="Value" Println ^Global(index) direct access to globals
    Exists(^a(1)) if value defined
    Traverse(^a("")) move to the next or previous subscript
    EraseValue, EraseArray, Merge, Lock, Unlock, Increment() miscellaneous functions
    Question:
    How do I work with Caché-specific structures such as List and Piece?
    Answer:
    There are a number of enhancements in Caché Basic for List and Piece support. For example:
    l = ListBuild("blue","red")
    Println List(l,1)
    
    p = "blue^red"
    noOfItems = Len(p,"^") 
    Println Piece(p,"^",1)
    
    Question:
    Is there any analog to $Order in Caché Basic?
    Answer:
    The Traverse() function provides the same functionality as $Order in Caché ObjectScript.
    i = Traverse(^MyData(""))
    While (i <> "")
        Println ^MyData(i)
        i = Traverse(^MyData(i))
    Wend
    
    Question:
    How do I work with objects in Caché Basic?
    Answer:
    Caché Basic and Objects Quick Reference
    Caché Basic syntax Objects reference
    "Basic.Human".ClassMethod() call class method
    obj.Report() instance method/property
    Me.Name="Anton" current object property/method
    obj=New Basic.Human() create new object
    obj=OpenId Basic.Human(1) open object instance
    Question:
    What is the analog to $this.Method or ..Method in Caché Basic?
    Answer:
    Me.Method
    Question:
    How do I work with SQL in Caché Basic?
    Answer:
    Use a dynamic query object:
    result = New %ResultSet()
    result.Prepare("SELECT Name,Age FROM Basic.Human WHERE Age<?")
    result.Execute(Arg1)
    
    While (result.Next())
        Println result.Data("Name") & ", " & result.Data("Age")
    Wend
    
    Question:
    How do I work with files in Caché Basic?
    Answer:
    Use %File object:
        file = New %File("c:\test.txt")
        file.Open("WSN")    
        file.WriteLine("This is a test")    
        file.Close()
    
    Question:
    How do I trap errors in Caché Basic?
    Answer:
    Use the On Error Goto statement:
    Function ErrorTest(Arg1)
      On Error Goto errorhandler
      return 1/Arg1
    
    errorhandler:
      PrintLn "Error ", Err.Number, " ", Err.Description, " ", Err.Source
      Err.Clear
      return 0
    
    End Function
    
    Question:
    Why don’t I get an <UNDEFINED> error in Caché Basic?
    Answer:
    In Basic, each variable is the empty string by default, so instead of an <UNDEFINED> error you get "" when referring to an undefined variable or function.
    Use the Option Explicit statement to avoid inadvertently referencing undefined variables and functions.
    Question:
    Can I call Caché ObjectScript programs from Caché Basic and vice versa?
    Answer:
    Yes, you can call both methods and functions/procedures, regardless of the language they are written in.
    In Caché ObjectScript:
     do Procedure^BasicRoutine()
     do ##class(MyClass).BasicClassMethod()
    
    In Caché Basic:
    Procedure@ObjectScriptRoutine()
    "MyClass".ObjectScriptMethod()
    
    Question:
    Why can’t I see local variables outside of the scope of my procedure?
    Answer:
    This is correct behavior; the Basic language defines this functionality. The scope of all variables is limited to the procedure or function where it was defined. Exceptions are variable names that start with the % symbol, such as %myvar.
    Question:
    How can I convert date and time values to and from $H format?
    Answer:
    Available conversion functions are: DateConvert(), TimeConvert(), and DateTimeConvert(). For example:
    myDate = "07/15/2002"
    println DateConvert(myDate, vbToInternal)   ' returns $H format
    myHDate = 59000
    println DateConvert(myHDate, vbToExternal)
    
    Question:
    Can I use indirection in Caché Basic?
    Answer:
    No.
    Support
    Question:
    I suspect there is a bug in Caché Basic. What should I do?
    Answer:
    Send a description of the bug to the InterSystems Worldwide Response Center (WRC).
    Question:
    I would like a particular feature in Caché Basic. Can InterSystems implement this?
    Answer:
    Send your ideas to the InterSystems Worldwide Response Center (WRC).