First, a little terminology. Classes or class modules are where objects are defined, just as Type statements are where UDT structures are defined. The actual object is an instance of the class. Classes define what kind of data the object can contain in its properties and how the object behaves through its methods.
Classes can range from very simple to vastly complex. Of course, it's best to start with the simple. This section shows you how to create an unsigned Integer data type using a class module. Chapter 5, "Object-Oriented Programming," covers more complex aspects of object-oriented programming.
Creating a New Data Type
The first section of this chapter showed you how to simulate unsigned integers in Visual Basic code. You can put that code into a UInt class to create a new, "fundamental" data type for working with unsigned integers.
The module level of the UInt class, shown below, defines the UDT structures and private variables used to return the high and low bytes from the integer. It's a good idea to specify the uses of a class, as well as to include comments within the class module. This makes the class more easily understood and more likely to be reused.
`Class UInt module level.
`Provides an unsigned integer type.
`
`Methods:
` None
`Properties:
` Value (default)
` HiByte
` LoByte
`Type structures used for returning high/low bytes
Private Type UnsignedIntType
lo As Byte
hi As Byte
End Type
Private Type SignedIntType
n As Integer
End Type
`Internal variables for returning high/low bytes
Private mnValue As SignedIntType
Private muValue As UnsignedIntType
The class uses the private module-level variable mnValue to store the actual value of the property, but access to this variable is controlled through the Let Value and Get Value property procedures, as shown in the following example. These procedures perform the conversions needed to simulate an unsigned integer.
Property Let Value(lngIn As Long)
mnValue.n = (lngIn And &H7FFF&) - (lngIn And &H8000&)
LSet muValue = mnValue
End Property
Property Get Value() As Long
Value = mnValue.n And &HFFFF&
End Property
Each property of the UInt class has a Let procedure in which the value of the property is assigned and a Get procedure in which the property value is returned. The HiByte and LoByte properties, for example, are implemented using Let and Get procedures, as shown here:
Property Let HiByte(bytIn As Byte)
muValue.hi = bytIn
LSet mnValue = muValue
End Property
Property Get HiByte() As Byte
HiByte = muValue.hi
End Property
Property Let LoByte(bytIn As Byte)
muValue.lo = bytIn
LSet mnValue = muValue
End Property
Property Get LoByte() As Byte
LoByte = muValue.lo
End Property
Finally, the UInt class has a public function that returns the signed integer value:
Function Signed() As Integer
Signed = mnValue.n
End Function
To use the UInt class, be sure to declare the object variable as New, as shown below. This creates a new instance of the class.
Dim uNewVar As New UInt
The Value property of the UInt class is the default property. (For information about how to set the default property of a class, see Chapter 5, "Object-Oriented Programming.") You can omit the property name Value when using the property, as shown here:
`Set the default property uNewVar = 64552
The HiByte and LoByte properties can be called just like any other Visual Basic property, as shown here:
`Set the high byte uNewVar.HiByte = &HFF `Return the low byte Print Hex(uNewVar.LoByte)