Can I define my own class modules and object modules?

Statistica Visual Basic supports class modules and objects modules. A discussion of classes and objects and the advantages (and some disadvantages) of object-oriented programming is beyond the scope of this introductory documentation; you should consult more advanced general sources on Visual Basic for details. In general, when developing large projects in Visual Basic, it is advantageous to impose as much structure on the program as possible, i.e., to break it down as much as possible into smaller functional units. This can be accomplished to a large extent by subroutines and functions; however, another way is by defining classes.

For example, you may want to use in your computations some highly customized way in which to compute some statistics to denote the central tendency for a dataset. One such statistic is, for example, the mean, but there may be additional considerations and problems that preclude you from using the simple arithmetic mean (e.g., censoring, missing data, highly skewed distributions, invalid data ranges, etc.). You may want to define a separate set of routines, options, etc., to compute the central tendency of a dataset. One way to do this is to create a class that contains the respective functions, subroutines, variables, etc. This class could for example compute the central tendency in one variable (e.g., variable CentralTendency), and the method that was used as a name in another variable (e.g., variable MethodName).

One of the advantages of using classes, as opposed to simple subroutines and functions, is that you can program all "things" related to the central tendency measure in a separate file. In the actual program, you would always access a variable called CentralTendency; even if the method for computing the central tendency is refined or variables are added to the class, etc., you don't have to rewrite the code that uses these measures at all. In other words, the class itself can be modified or refined, or another team of programmers could develop it, while the programs that use the class do not have to be modified at all.

Classes vs. objects.
The difference between classes and objects is that classes have to be explicitly declared (instantiated) in the program that uses them, while objects are automatically instantiated and available at all times. Both of these will be illustrated in a simple example below.
The asterisk (*) convention: Writing portable programs
When writing programs that are to be deployed on other computers, you can use the asterisk (*) convention to access the include files that contains your classes and objects. Specifically, after you write your class or object modules, save them either in the directory where Statistica is installed, in the directory where the current module is running from (if it has been saved), or in the global macro directory. To access modules from within an SVB program, you can then use a statement such as #Uses "*MyClassModule.svc" to access the previously saved class module MyClassModule.svc; therefore, you do not have to use the explicit reference to the respective directory (e.g., #Uses "J:\Statistica\Examples|Macros\MyClassModule.svc"). To summarize, by using the asterisk (*) convention with the #Uses or $Include directives, you can make your library inclusion statements in your applications portable.
A simple example
Suppose you wrote a program to retrieve some keys (key-codes) or passwords to gain access to particular datafiles or programs. The main program does nothing else but access the class (or object) that generates those keys. The method in which those keys are computed is entirely "encapsulated" inside the respective class (or object).

Create the following three files as Statistica Visual Basic macro programs. The first one defines Class as the Macro Type (when creating the new Visual Basic document); call this program KeyCodesClass. The screen below shows the selections from the Create New Document and the Macro Type dialogs to create the KeyCodesClass module.

The second file defines Object as the Macro Type; call this program KeyCodesObject. As a third file, create a standard Statistica Visual Basic macro program, and call it RetrieveKeyCodes; note that only this latter program will automatically have the Sub Main ... End Sub declarations inserted into the code.

Now enter the following code into these programs:

The Class Module: KeyCodesClass

' This file contains the code that defines the ' KeyCodesClass,i.e., the functions and subroutines ' that belong to this class. This file should be created ' (or already exists) as: ' J:\Statistica\Examples\Macros\KeyCodesClass.svc ' Variable KeyCode is defined globally inside this ' file; it can be accessed by the class member functions ' and subroutines.

Dim KeyCode As String

Private Sub Class_Initialize()

' This function is called when you initialize the class. ' Here you could put the code to generate the keycodes ' for various versions, etc. ' For the example, we will simply set it to a constant ' string.

KeyCode = "ThisIsMyCodeFromTheClass"

End Sub

Private Sub Class_Terminate()

' This function is always called when you terminate ' the program, and this class. You could, for example, ' put some code here to update some data base, or internal ' counters that keep track of how many times the key codes ' program (class) has been used.

MsgBox "KeyCodesClass is terminating."

End Sub

Function RetrieveCodeFromClass As String

' This is the routine that will be called to ' retrieve the code from this class.

RetrieveCodeFromClass=KeyCode

End Function

The Object Module: KeyCodesObject

' This file contains the code that defines the ' KeyCodesObject,i.e., the functions and subroutines ' that belong to this object. This file should be created ' (or already exists) as: ' J:\Statistica\Examples\Macros\KeyCodesObject.svo ' Variable KeyCode is defined globally inside this ' file; it can be accessed by the member functions ' and subroutines.

Dim KeyCode As String

Private Sub Object_Initialize()

' This function is called when you initialize the object. ' Here you could put the code to generate the keycodes ' for various versions, etc. ' For the example, we will simply set it to a constant ' string.

KeyCode = "ThisIsMyCodeFromTheObject"

End Sub

Private Sub Object_Terminate()

' This function is always called when you terminate the ' program, and this object. You could, for example, ' put some code here to update some data base, or internal ' counters that keep track of how many times the key codes ' program (object) has been used.

MsgBox "KeyCodesObject is terminating."

End Sub

Function RetrieveCodeFromObject As String

' This is the routine that will be called to ' retrieve the code from this object.

RetrieveCodeFromObject=KeyCode

End Function

The main program: RetrieveKeyCodes

' This program illustrates the use of classes and ' objects in Statistica Visual Basic. The following ' two lines include the references to definitions ' for a class module and an object module, respectively. ' (Files containing a class module have the extension ' .svc; files containing an object module have the ' extension .svo) ' NOTE: The directory in which these files are located may ' be different in your installation of Statistica. '#Uses "J:\Statistica\Examples\Macros\KeyCodesClass.svc" '#Uses "J:\Statistica\Examples\Macros\KeyCodesObject.svo"

Sub Main

' Because KeyCodesClass is a Class, an object of this type ' has to be explicitly created (initialized).

Dim Keys As New KeyCodesClass

MsgBox Keys.RetrieveCodeFromClass

' KeyCodesObject is an object module, so it already exists. ' We can simply call it. ' MsgBox KeyCodesObject.RetrieveCodeFromObject ' The following line will explicitly destroy ' this instance of the KeyCodesClass, i.e., call ' the destructor function for that class.

Set Keys = Nothing

End Sub

Before you run this program, make sure that the respective class and object module files exist in the correct locations; note that you could also save all files in the default directory for global macros (e.g., in the root directory of the Statistica installation on your machine), and then use the asterisk convention to reference the files, e.g., via #Uses "*KeyCodesClass.svc". Two different "key-codes" will be retrieved. Suppose that a very elaborate (and perhaps confidential) method exists to generate the key codes. By using object (or class) modules, you could encapsulate inside the object (or class) the method for creating the keys; this method could then be modified, updated, etc., while the main program that retrieves those keys by accessing the object (class) would not have to change.