ZBasic Language Reference
107
ZBasic Microcontrollers
Public Sub _Create()
Call SetTime()
End Sub
Public Sub _Create(ByVal h as Byte)
Call SetTime(h)
End Sub
Public Sub _Create(ByVal h as Byte, ByVal m as Byte)
Call SetTime(h, m)
End Sub
Public Sub _Create(ByVal h as Byte, ByVal m as Byte, ByVal s as Single)
Call SetTime(h, m, s)
End Sub
Public Sub SetTime(ByVal h as Byte = 0, ByVal m as Byte = 0, _
ByVal s as Single = 0.0)
hour = Min(h, 23)
minute = Min(m, 59)
Seconds = Min(s, 59.999)
End Sub
Public Function GetHour() as Byte
GetHour = hour
End Function
Public Function GetMinute() as Byte
GetMinute = minute
End Function
Public Function GetSeconds() as Single
GetSeconds = seconds
End Function
End Class
Dim t as MyTime = _Create(12)
Sub Main()
Debug.Print t.GetHour()
End Sub
Of course, in this particular case the four separate constructors could be replaced by a single constructor
that uses default parameter values. (Just as with regular ZBasic procedures, the compiler automatically
inserts the specified default value if you omit one or more of the right-most parameters on a particular
invocation.) If that were done, the constructor would have the form shown below.
Public Sub _Create(ByVal h as Byte = 0, ByVal m as Byte = 0, _
ByVal s as Single = 0.0)
Call SetTime(h, m, s)
End Sub
When you define an array of objects, you may specify different constructors for each element of the array
as shown in the example below. In this particular case, explicit constructor invocations were specified
only for three of the ten elements of the array of objects. The remaining elements are initialized using the
default constructor one that can be invoked with no parameters. You may also specify a constructor list
for multi-dimension arrays. In that case, the constructors are applied sequentially to the elements of the
array with the leftmost index varying the fastest. This corresponds with the way that arrays are laid out in
memory.
Dim t2(1 to 10) as MyTime = { _Create(12), _Create(), _Create(1, 2) }
|