108 of 208
ZBasic Language Reference 100 ZX Microcontroller Family The syntax for a member definition is given by the two descriptions below – the first being for a non-array member and the second being for a member that is an array. {Public | Private | Dim} <name> As <type> or {Public | Private | Dim} <name>(<dim-spec-list>) As <type> As with ordinary variables, Dim has exactly the same effect as Private. The names of the members of each class may be any legal identifier but a particular name may be used only once in each class. The use of a name as a member in one class does not preclude it from also being used as a member name in a different class or structure or as a variable, constant, parameter, etc. The <type> specified for a member may be any of the pre-defined types like Integer, Byte, String, etc. or it may be a user-defined type like an enumeration, a structure or another class. It is important to note that recursive class definitions, with members that are or contain (directly or indirectly) an object of the class being defined, are not allowed. Defining a class that contains one or more members that are of different class types, known as composition, is an important aspect of object-oriented programming. Examples Class MyDate Public year as UnsignedInteger Public month as Byte Public day as Byte End Class Class MyTime Public hour as Byte Public minute as Byte Public seconds as Single End Class Class MyTimeStamp Public tdate as MyDate Public ttime as MyTime Private isCurrent as Boolean End Class Sub Main() Dim ts as MyTimeStamp Call GetTimeStamp(ts.tdate.year, ts.tdate.month, ts.tdate.day, _ ts.ttime.hour, ts.ttime.minute, ts.ttime.seconds) End Sub The example above emphasizes the similarities between structures and classes; except for the different keyword used, the syntax is identical. As with structures, instances of classes (called objects) can be passed to subroutines and functions either by reference or by value. If an object is passed by value, it will be read-only within the receiving procedure. A variable that is an instance of a class may be assigned to another variable that is an instance of the same class using the standard assignment operator. If the class has members that are an allocated string type or other objects, special, automatically generated “copy procedures” are invoked to perform the copying. This achieves the desired result in most cases, but there are certain special cases where the compiler cannot produce a logically correct copy using this strategy. For such cases, there is a way to specify how the object should be copied. See the discussion about assignment constructors in Section 4.6.