Start Back Next End
  
ZBasic Language Reference
105
ZBasic Microcontrollers
Two variables that are the same type of object may be compared for equality or inequality using the
standard comparison operators, = and <>.  As with structures, the equality/inequality test is implemented
using a byte-by-byte comparison of the content of the two objects.  If one or more members of the object
are the BoundedString type, the byte-by-byte comparison may result in a False value even though the
strings are identical.  This is because the currently-unused portion of the string store may contain byte
values that are different between the two instances being compared.  Similarly, comparison of objects
containing allocated strings, while allowed, is not recommended because of the likelihood of producing
false negative results. 
4.3 Defining Class Methods
So far, the discussion of classes has been limited to the similarities between structures and classes. 
However, classes, and instances of classes (objects), are much more powerful than structures.  Most of
the additional capability results from the ability to associate subroutines and functions with the class.  In
the parlance of object-oriented programming, a subroutine or function that is part of a class definition is
known as a method.  We will generally refer to the class’ subroutines and functions as methods unless
the context requires a clear distinction between a method that is a subroutine and one that happens to be
a function.
A method is defined as being part of a class simply by including the desired subroutine/function definition
within the class definition, right along with all of the class data members.  Consider this simple example.
Class MyTime
Private hour as Byte
Private minute as Byte
Private seconds as Single
Public Sub SetTime(ByVal h as Byte, ByVal m as Byte, ByVal s as Single)
hour = h
minute = m
Seconds = s
End Sub
End Class
This simple class has three data members and one one method, whose ostensible purpose is to set the
data members of the object to specific values.  A class method is invoked the same way as any “normal”
subroutine or function except that the name of the object to be acted upon must also be specified.  The
example below, which utilizes the class defined above, illustrates how this is done.
Dim t as MyTime
Sub Main()
Call t.SetTime(12, 0, 0.0)
End Sub
In this example, the variable t is an instance of the class MyTime.  In the parlance of object-oriented
programming, t is said to be an object of type MyTime.
So far, the example class isn’t particularly useful.  It has a method for setting the data members of the
object but, because the data members are defined to be Private, they can’t be accessed by procedures
other than class methods.  Making data members private is an important part of the concepts of
information hiding and encapsulation that are central to object-oriented programming.  The general idea is
that you make most (preferably, all) data members private and then provide accessor methods to return
data values from the object to the caller and also provide mutator methods to allow a caller to modify an
object’s data members indirectly.
The SetTime() method above is a mutator method that happens to provide the ability to modify all of the
data members in a single call.  Depending on the nature and complexity of the classes that you define, it
may be preferable to define mutator methods that modify individual data members, subsets of the class’
data members, or all data members.  One of the advantages of providing mutator methods for setting
data member values is that the mutator method can enforce limits on values of the data members or
Previous page Top Next page