With JoPPS v2 JoPPS-Script offers the ability to instanciate object variables that can be used
to perform various tasks such as writing to a diskfile or accessing the Windows registry.
It is not possible, nor is it intended, to create new object classes - or to derive your own object classes.
The classes provided in JoPPS-Script are only intended to be used in your scripts: they group methods and properties logically belonging together into a single variable.
Object classes
The standard v2 syntax comes with the following predefined object classes :
- STRINGS class: handles lists of strings (eg. texts)
- REGISTRY class: interfaces the windows registry
- INIFILE class: interfaces windows INI-files
- HANDLE class: interfaces windows filehandles (e.g. a diskfile or a named pipe)
Refer to 2.7.3. Predefined object classes for a detailed discussion on these object classes.
In JoPPS itself various object classes are added to the list:
- to interface the JoPPS database
- to work with projects and projectdata
- to interface the modellibrary
- to program simple dialogs to interact with the user
- to interface machining centers
Fore more information about working with objects in JoPPS refer to 3.5 JoPPS related objects.
Simple dialogs can be created using the Form Objects. Refer to 4. Talking to the user : Form objects for more
information about creating dialogs.
Object variables : instanciating and destroying
To use an object start with instanciating an object variable : allocating memory to hold the object and initialize its inner workings.
This can be done by calling an object's constructor Create.
oText := Strings.Create(); /* a constructor is a method so it needs () */
After the call the variabele oText holds an instanciated STRINGS object. If memory allocation fails the
variabele oText will be Nil and cannot be used. We can test for a Nil object using the function
IsNil:
if IsNil(oText) then Halt; /* Oops, something went wrong here.. */
or by
if oText = Nil then Halt;
Note that IsNil can also be used on non-object variables.
Calling methods or addressing properties of a nil object will cause a run-time error.
Do not instanciate objects of classes where the classname begins with and underscore character !
These classes are for internal use only, they can usually be accessed by means of a system variable.
We are now ready to use our newly instanciated object; by first testing for a Nil value we made sure
we are now dealing with a valid object.. We are ready to use its properties and call its methods:
if oText.LoadFromFile("c:\autoexec.bat") then
ShowMessage("My autoexec.bat file contains "+IntToStr(oText.Count)+" lines of text")
else
ShowMessage("You have no autoexec.bat !");
We can assign an object variable (O-type variables) like any other variable:
oText2 := oText;
Now the variable oText2 points to the same object as oText does; objects are in fact pointers so
assigning object variables just DUPLICATES THE REFERENCE, NOT THE OBJECT!
We can now use oText2 in the same way we use oText. Both variables reference the same object
instance.
Instanciated objects occupy dynamic memory and should be freed when no longer needed. Free an object
by calling its destructor Free:
oText.Free(); /* a destructor is a method so it needs () */
Once the object is freed the variable oText (and oText2) will be Nil.
Using a Nil object will trigger a runtime error. Executing the next statement raises a runtime error
as oText2 is Nil (because we freed oText and oText2 pointed to the same object instance) :
oText2.SaveToFile("c:\autoexec.bak"); /* This will give a runtime error */
Freeing an object garantees any windows resources (e.g. a filehandle) allocated by the object are properly released.
Normally object instances are freed automatically by JoPPS-Script when your script terminates but it is
good policy to free all objects you instanciate.
Predefined object classes
The V2 syntax comes with a number of build-in object classes. You can uses these classes in JScripter or JoPPS.
The ARRAY class
The INIFILE class
The HANDLE class
The PARAMETER class
The REGISTRY class
The STRINGS class