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
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.