Defining Class Modules and Object Modules
Statistica Visual Basic supports user-defined 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 data set. One such statistic, of course is 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 data set. 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, refined, or it could be developed by another team of programmers, 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 use them, while objects are automatically instantiated and available at all times. Both of these will be illustrated in a simple example below.
- A simple example
- Suppose you wrote a program to retrieve some keys (key-codes) or passwords to gain access to particular data files 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. First select New from the File menu to display the Create New Document dialog box. On the Create New Document dialog box - Macro (SVB) Program tab, type KeyCodesClass as the Name. Next, to define the macro as a class module, click the Macro Type button to display the Macro Type dialog box. In the Macro Type dialog box, select Class as the Macro Type. The screen below shows the selections from the Create New Document and the Macro Type dialog boxes to create the KeyCodesClass macro.
In the same manner, name the second file KeyCodesObject and define it as an object macro (selecting Object as the Macro Type). Finally, name the third file RetrieveKeyCodes and define it as a standard macro (selecting Standard as the Macro Type); 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: ' C:\Program Files\Statistica\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: ' C:\Program Files\Statistica\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 "C:\Program Files\Statistica\Statistica *\Examples\Macros\KeyCodesClass.svc" '#Uses "C:\Program Files\Statistica\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
When you run this program, after you make sure that the respective class and object module files exist in the correct locations, 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.