Performing Computations, Data Types, Subroutines, Functions

The following program illustrates how variables (for storing numbers or text) and arrays can be declared and used. It also illustrates how subroutines (functions) can be declared and how arguments can be passed to functions. In Statistica, select File New. In the Create New Document dialog box, select the Macro (SVB) Program tab and create a macro called Overview 2.

' This program will compute the sum of the

' squared values from 1 to 10. The program

' illustrates how different numeric and string

' data types are declared and handled in

' Visual Basic. The program also illustrates

' how subroutines (functions) are declared.

Sub Main

' Declare x as an array of double values;

' there are 10 elements in this array, which

' can be addressed as x(1), x(2), etc. (i.e.,

' the lower bound of the array is element 1,

' the upper bound is 10).

Dim x (1 To 10) As Double

Dim Sum As Double, ResText As String

Dim i As Integer

' Here a For...Next loop is used to fill

' array x with values between 1 and 10

For i =1 To 10

x(i)=i

Next i

' A function is called to compute the sum of

' the squared values in array x; note how

' we do not pass explicitly the lower and

' upper bounds for array x, but use functions

' (LBound, UBound) to retrieve those values.

Sum=ComputeSumOfSqrs ( LBound (x), UBound(x), x)

' Finally, we put together a string value (text)

' with the results that will be passed to a

' Message Box

ResText="The sum of the square values " + _
 Str(LBound(x)) + " to " + Str(UBound(x)) + _
 " is " + Str(Sum)

MsgBox ResText

End Sub

' This function computes the sum of the squared

' values in an array of double numbers. The

' function returns a double value.

Function ComputeSumOfSqrs (iFrom As Integer, _
 iTo As Integer, x() As Double) As Double
  

Dim i As Integer

ComputeSumOfSqrs=0

For i= iFrom To iTo

ComputeSumOfSqrs=ComputeSumOfSqrs+x(i)^2

Next i

End Function

Numbers
The Double data type and the Integer and Long data types are probably the ones most commonly used in computations. Variables declared as Double can hold (store) real numbers, approximately in the range from +/-1.7E +/- 308 (approximately 15 digits of precision); variables declared as Integer can hold (store) integer numbers in the range from -32,768 to 32,767, and Long  variables can hold (store) integer numbers in the range from -2,147,483,648 to 2,147,483,647.
Strings
Use the String data type to operate on character strings of arbitrary length.
Boolean
The Boolean data type can hold two values: True (1) or False (0).
Arrays
The example program also illustrates how arrays of values are declared and used in Visual Basic. By default, arrays are 0-referenced; this means that an array declared as Dim x(5) actually has 6 elements: The first element can be referenced as x(0), the second as x(1), and the 6th element as x(5). You can also declare arrays with explicit boundaries; for example, you declare an array as Dim x(1 to 5), then it will only have 5 elements, with the first element referenced as x(1). You can also place at the beginning of the program the statement Option Base 1, which will by default declare all arrays as 1-referenced, i.e., with a lower array bound of 1.
Assigning objects to variables
When assigning objects to variables (see Objects, Methods, and Properties), you need to use the syntax Set Variable = Object; for example:

Sub Main

Dim wb As Workbook

Dim ss As Spreadsheet

Dim g As Graph

Set wb = ActiveWorkbook

' Note that the _ (underline) at the end of a

' line can be used to continue a statement to the

' next line.

Set ss=Spreadsheets.Open( _
 Path & "\Examples\Datasets\Adstudy.sta")

ss.Visible = True

Set g = ActiveGraph

End Sub

See also How to Specify Arrays in Statistica Visual Basic Functions, Array Indexing, and Statistica Visual Basic Library of Matrix Functions - Dim for additional information regarding arrays.