Although the FRED scripting language is technically not Visual Basic, it is very similar and for all practical purposes can be considered the same. In the remainder of this article we will refer to FRED's scripting language as "VB". It is the intent of this article to introduce some of the very basic differences between VB and C++ from the perspective of a programmer familiar with C++. It is not the intent of this help article to teach the basics of VB.
For general information on VB and the variation of BASIC that FRED uses for its scripting language (WinWrap by Polar Engineering) please see the links below. WinWrap BASIC by Polar Engineering
Generic documentation on WinWrap BASIC can be found from within FRED by going to Help > Scripting Help > BASIC Language Reference.
Specific documentation of the FRED Extension commands used to construct FRED scripts can be found in the Scripting Reference Manual help topic.
Documentation on the File System Object, which can be called from a FRED script and used to process drives, folders and files on your computer, can be found here. Documentation on the Dictionary Object, which can be called from a FRED script in order to construct an associative array, can be found here.
Whereas C/C++ has only functions that take arguments in parentheses and return a value (unless returning a void), VB has both functions and subroutines. VB functions are called in the same way as C/C++, for example, a function "MyFunc" which returns an value "n" and takes a single argument "i" is written as:
n = MyFunc( i )
However, subroutines are different in a subtle way because the subroutine arguments are not enclosed in parentheses and they return no value. A subroutine "MySub" which takes two arguments "i", and "j" is written as:
MySub i, j
VB is a Weakly Typed Language (the "Variant" type) VB is a weakly typed language. In fact, the default behavior is that variables do not have to be declared before they are used. For example,
x = 7.0 i = 13 s = "this is a string" Print x, i, s
Variables used in this way are of type VARIANT and will automatically adjust their type depending on how they are used. This feature of VB can be very handy but can also present problems when variables are used as arguments to FRED-specific functions. Most of these functions require a specific data type for each argument, as is documented in the corresponding help reference article for the function.
You can use the "Dim" statement to force a variable to be a specific type.
Dim x As Double
or you can specify its type in an assignment statement if using Option Explicit Off (see next section).
x# = 7.89e-4
The "Dim" Statement and "OPTION EXPLICIT" The "Option Explicit" statement forces every variable to be explicitly declared before it can be used. Regardless of whether or not you have the "Option Explicit" statement we recommend using the "Dim" statement to declare variables.
Dim x As Double, Y As Float, S As String
NOTE: Even though the syntax
Dim X#, Y!, S$
is allowed, Photon Engineering strongly recommends AGAINST using this form for Dimension statements due to possible conflicts with structure members. In order to allow use of this form, the declaration "Option Explicit Off" must be included in the script.
For reference, the short-hand syntax mapping is the following: # - Double ! - Single & - Long % - Integer $ - String ? - Portable Integer @ - Currency
One difference between VB and C/C++ is illustrated below. The following two statements are NOT equivalent!
Dim X As Double, Y As Double, Z As Double Dim X, Y, Z As Double
In the first example X, Y, and Z are of type Double. But in the second example Z is of type Double, while X and Y are of type Variant. The difference is subtle and can sometimes cause unintended behavior.
Array Declarations and "OPTION BASE" The specification of array bounds in VB can be a very subtle frustration for C/C++ programmers. By default the array indexing is zero-based as in C/C++, but in the following Dim statement
Dim X(10) As Double
the number 10 is the UPPER BOUND of the array, NOT the number of elements as in C/C++. X is an array of 11 double elements.
If you want an array with 10 elements, then do the following
Dim X(9) As Double
If you would like your array indexing to be one-based, then use the "Option Base 1" statement.
Boolean values are specified by True and False. The default value for uninitialized Booleans is False. Dim b As Boolean b = True
In a script editor window, References to the type libraries from other programs (ex. Matlab, Excel) can be added so that the editor is "aware" of the method definitions that are available to it when using an object instance of that program. Furthermore, these References are saved with the script so that they do not have to be re-loaded each time the script is opened.
References are added to a script by right mouse clicking in the script editor window and selecting Edit > References from the context menu.
References saved with a script can be confirmed by:
|