Please enable JavaScript to view this site.

DAQFactory User's Guide

Navigation: 5 Sequences & Scripting > 5.20 Object Oriented Programming

5.20.14 OOP Notes

Scroll Prev Top Next More

Object references must be stored in numeric variables, not string variables.

Class definitions are created at compile time.  That said, the simplest way to ensure that the sequence is compiled and therefore the class defined is to actually run the code.  Running class definition code won't do anything, and really shouldn't affect performance either.

Classes, along with their member functions and variables are listed in the workspace under the sequence that created them.  You can click on a member function and it will jump to that function in the script editor.

You cannot combine scalars and object references in the same array.  If People = 0 and you do People[1] = new(Person) then the entire People array will be assumed to be object references.  Since People[0] is still 0, which is certainly an invalid reference, you will get errors trying to access People[0].  Of course you can then assign People[0] to an object.  Likewise the other way.  If People[0] = new(Person) and you do People[1] = 0, you will lose the object reference in People[0].

When an object reference is passed to a function, that reference is copied, just like ref1 = ref2, and so it basically appears that the object is passed by reference.  This is the opposite of regular variables which are always passed by value.  In fact, if you need to pass a regular variable by reference, create a basic class with one member variable, instantiate it and pass that to the function.

Object references are technically just a number, however, you cannot perform any math function on them.  In fact, the only functions you can perform on them are == and !=, and of course the various functions that don't affect the value itself, such as InsertTime, ShiftTime, Point, etc.  Doing ref1 == ref2 will return 1 if both ref1 and ref2 point to the same object.   Of course member variables of objects are like any other variable.

Like the rest of DAQFactory, you can change class definitions on the fly.  However, once a class has been instantiated into an object, any changes to the local variable declarations will not affect the instantiated object.  You will have to reinstantiate the object to pickup the changes.  However, as we'll see later, changes to member functions of a class will affect all instantiated objects immediately without having to recreate the objects.  "Immediately" is not quite completely accurate though.  If an object is currently executing a member function and you change the class definition for that function while it is executing that function, the update will not take effect until the function has ended.  This is similar to changes in sequence code.  The reason changes in local variables require reinstantiation and member function changes don't is that member variables are created when the class is instantiated using the pattern of the class definition, but are stored in the object.  Member functions are only stored in the class definition and a copy only made when the function is called.  That said, one way to add a local variable without having to reinstantiate all your objects is to declare it inside a member function instead of in the general class and then call that new function.

Although many of the built in DAQFactory functions appear to be in objects, such as File., DB., Email., Component., etc., you cannot derive from these objects.  You can only derive from your own objects.

Objects are referenced counted, so remain in memory until the last reference is lost.  A reference is lost either by going out of scope (a private variable), setting the reference to another reference or scalar, doing ClearGlobals(), or of course quitting DAQFactory, starting a new document, or opening another document.  A reference is created by new(), setting a reference equal to another, i.e. ref1 = ref2, or by passing the reference to a function.

In order to copy an object, that is create a new object with the same local variables, you will need to manually transfer the locals from your existing object to the new object.  We recommend creating a copy member function.  For example, we could add this to our Person class:

 

function CopyTo(newclass)

    newclass.name = name

    newclass.address = address

    newclass.zip = zip

endfunction

Then, if we wanted to make a copy, we'd do something like this:

 

NewCopy = new(Person)

People[1].CopyTo(NewCopy)

You can also do this using tojson() and from json(), but with a little less control over what gets copied:

 

NewCopy = fromJson(People[1].toJson())

Remember, simply doing NewCopy = People[1] will simply make NewCopy contain another reference to the same object that People[1] refers to, which is not the same thing.