Ole4D Scripting, How To

Transferring data between VBS and 4D




A)Transferring data to VBS
The simple types(double, longint..) that are easy to convert to string are easy to transfer to VB.
For the complex types like arrays and blobs you will have to write methods for converting them to their appropriate VBS representation.

Steps for transferring a the value of a 4D variable to VBS:
  • Convert the 4D variable to string. This string must have VBS syntax.
  • Insert the string obtained into the VBS code


Examples:
  1. Transfer of simple types:
    ` computing example for number e with the precision eps
    ` using the formula 1+1/1!+1/2!+1/3!+1/4!+...
    ` the approximation is given by a 4D variable
    eps := 0.01
    $s := "eps="+String(eps)
    ` the line of code for data transfer follows
    $i := OCRunScript("VBHost";$s);
    If ($i#0)
    ` an error occurred
      OCLastError($num;$sysMsg; $datailedMessage;$errLine;$errChar;$errCodeLine)
      $err := "VBS error: "+$datailedMessage+Char(13)
      $err := $err+"line:"+String($errLine)+ " char:"+$errChar
      ALERT($err)
    End If
  2. Transfer of strings.
    When strings are involved you have to remember that quotation marks in VBS strings are doubled: ` the ascii code for quotation mark is 34
    `in this example "Hello World!" is surrounded with quotation marks
    $s := Char(34)+"Another "
    $s := $s+Char(34)+Char(34)+"Hello World!"+Char(34)+Char(34)
    $s := $s+" program"+Char(34)+Char(13)
    $s := "MsgBox "+$s
    $i := OCRunScript("VBHost";$s)
    If ($i#0)
    ` an error occurred
      OCLastError($num;$sysMsg; $datailedMessage;$errLine;$errChar;$errCodeLine)
      $err := "VBS error: "+$datailedMessage+Char(13)
      $err := $err+"line:"+String($errLine)+ " char:"+$errChar
      ALERT($err)
    End If


    Here is the code of a 4D method which converts the 4D Strings into strings that can be added to VBScript code:
    C_TEXT($1;$0)
    $0:=$1
    $0:=Replace string($0;Char(34);Char(34)+Char(34))
    $0:=Replace string($0;Char(13);Char(34)+"+chr(13)+"+Char(34))

  3. Example of a 4D array transfer to VBS.
    ` a 4d array of longint is transferred into a VBS array
    $size := Size of array($a)
    $s := "Dim a("+String($size)+")"+Char(34)+Char(13)
    `add each element of the 4D array to VBS
    For($i;1;$size)
      $s := $s +"a("+String($i-1)+") = "+String($a{$i})+Char(13)
    End for



B)Transferring data from VBS to 4D

Transfer of data to 4D can be done only by assigning 4D variables from VBS.
To assign values from VBS in 4D you have to use SHost object, or one of the following plug-in commands:
- for event handling methods : OCGetLongOfSender , OCGetRealOfSender ,OCGetTextOfSender , OCGetParamCount , OCGetRealParam , OCGetLongParam ,OCGetTextParam .
- for regular 4D methods : OCGetRealProperty , OCGetLongProperty ,OCGetTextProperty .
Only the simple VB Script data types: double, longint and string can be transferred to 4D using SHost object.
For each of the types double, longint and string SHost has a method that can be used to assign a VBS value to a 4D variable. This kind of methods have two parameters. First argument is a string with the name of the 4D variable and the second is the value you want to assign.
Similar to transferring data from 4D to VBS(explained in part A) of this help page) , complex data from VBS can be converted to integers, strings or doubles and used in 4D.

All the 4D variables that are assigned from VBS must be process or interprocess variables, and they must be also declared.
Example:
The following assignment, of a value from VBS to a 4D local variable, is invalid.
C_REAL($d)
$s = "SHost.SetDouble "+Char(34)+"$d"+Char(34)+", 3.14"
$i := OCRunScript("test";$s)


The correct way is to assign a value to a process or interprocess variable.
C_REAL(d)
$s = "SHost.SetReal"+Char(34)+"d"+Char(34)+", 3.14"
$i := OCRunScript("test";$s);