Start Back Next End
  
ZBasic Language Reference
118
ZBasic Microcontrollers
Call b1.ShowWeight()
Call b1._Destroy()
Call System.Free(addr)
End Sub
Another use for a based object is to be able to treat a base class object as if it were a derived object. 
Here again, it is your responsibility to ensure that such treatment is appropriate.  The compiler will not
issue any error messages or warnings about incompatible classes.  As an example, consider the
subroutine below.
Sub IdentifyObject(ByVal obj as A)
Call obj.Identify()
' Define an object based on the object passed
Dim objB as B Based obj.DataAddress
Call objB.ShowWeight()
End Sub
If we knew, in certain cases, that the passed object were actually of class B (derived from class A), we
could use a based object definition to be able to access the unique methods and data members of the B
class.  This capability is rarely needed but it is good to keep it in mind for those cases where it is.  It is
important to distinguish this particular use of a based object from the use case given in the first example
in this section.  In this use case the object is assumed to be validly constructed and, therefore, the
constructor/destructor should not be invoked on the object as it is in the first example where the based
object was defined as overlaying memory of undefined content.
One final note, you may optionally include the Const keyword between the class name and the Based
keyword to define a read-only based object.  With such a read-only based object definition, access is
limited to Const methods and, moreover, data members may not be modified nor passed by reference to
other procedures.
The ideas described above apply equally well to objects defined ByRef.  Consider the example below
that is a variation of the first example given above.
Dim buf(1 to 20) as Byte
Sub Main()
Dim b1 as B ByRef
b1.DataAddress = buf.DataAddress
Call b1._Create(5, B::Blue, 25)
Call b1.ShowWeight()
Call b1._Destroy()
End Sub
4.17 Miscellaneous Class Elements
Class data members may be defined that have the ProgMem or Persistent attributes but in either case,
the data member must be defined as Static so that there is only one instance of the data item.  Such
members may be either Private or Public as needed. 
A Declare statement may be placed in a class definition.  It may be either Public or Private but it must
have the Static attribute.
You may define constants, enumerations and structures within a class definition.  Such definitions may be
private if it is desirable that they are only available to class methods or they may be public if that is useful.
Previous page Top Next page