- The Class Hierarchy
- Creating a New Class
- Declaration and Instantiation
- Constructors and Destructors
- Garbage Collection
- Object Operators
- Adding and Overriding Methods
- Calling the Overridden Method
- Access Scope: Public, Private, Protected
- Setting Properties with Methods
- Default and Optional Parameters
- Declaring Variables Static and Const
- Revisiting the StringParser Module
- Example: Creating a Properties Class
- Data-Oriented Classes and Visual Basic Data Types
- Advanced Techniques
Data-Oriented Classes and Visual Basic Data Types
In the previous chapter, I said that some data types in Visual Basic don't exist in REALbasic as data types. The reason is that REALbasic often has an object-oriented alternative. For example, an intrinsic data type in Visual Basic is a Date, but in REALbasic it's a class. In other situations, I said that similar functionality could be implemented with the MemoryBlock class. Now that you know about classes, I can review these two classes in more detail and complete the discussion of data types and how REALbasic compares with Visual Basic. I will also review the Collection class, because it is very similar to the Dictionary class. REALbasic recommends sticking to the Dictionary class for new projects and retains the Collection class only for compatibility with Visual Basic and earlier versions of REALbasic.
Date is an intrinsic data type in Visual Basic, but it's a class in REALbasic. It measures time in seconds since 12:00 a.m., January 1, 1904. The total number of seconds since that time is how a particular date and time are identified, and it is stored in the TotalSeconds property of the Date class as a double.
The Variant type treats Dates in a special way to help maintain compatibility with Visual Basic. The VarType() function returns a 7 as the data type for dates, rather than 9, which is what is typically done with objects. Basically, all the Variant is doing is storing the TotalSeconds property as a double.
There is a ParseDate helper function that accepts a date formatted as a string and parses it into a Date object. It returns a Boolean representing success or failure in parsing the string that was passed to it. If it returns a Boolean, how then do you pass it a string and get a Date object back? The function signature looks like this:
REALbasic.ParseDate(aDate as String, aParsedDate as Date) as Boolean
The answer is that objects, like Date, are passed by reference, rather than by their value. I wrote earlier that variables that were one of REALbasic's intrinsic data types were called scalar variables. These variables refer to the value held in memory, whereas variables that referred to objects were references to the object, like a pointer to a particular section of memory. When you pass the Date object to the ParseDate function, the ParseDate function does its work on the Date object passed to it so that when it changes a value of the Date object, that change is reflected in the original variable that was passed to the function.
Dim d as Date Dim s,t as String Dim b as Boolean s = "12/24/2004" b = ParseDate(s, d) // b is True t = d.ShortDate // t = "12/24/2004"
Note that all you have to do is Declare d as a Date; you do not need to instantiate it prior to passing it to the ParseDate function.
You can also do the same trick with scalar variables if you use the ByRef keyword in the function signature. There are actually two keywords here—ByRef and ByVal—and scalar variables are, by default, passed ByVal.
The acceptable formats of the string are the following:
1/1/2006 1-1-2006 1.1.2006 1Jan2006 1.Jan.2006 1 Jan. 2006
TotalSeconds reflects how the date and time are actually stored in a Date object. When you instantiate a new Date object, it automatically gets set to the current date and time.
Date.TotalSeconds as Double
You can also get or set different components of the time with the following properties. Changes made to these will be reflected in TotalSeconds:
Date.Second as Integer Date.Minute as Integer Date.Hour as Integer Date.Day as Integer Date.Month as Integer Date.Year as Integer
The following two methods set the date or date and time using the standard SQL format used in databases. This comes in handy when dealing with databases.
Date.SQLDate as String
You can get or set the date using the format YYYY-MM-DD.
Date.SQLDateTime as String
You can get or set the date using the format YYYY-MM-DD HH:MM:SS.
These properties are all read-only and return the date or time in different formats, according to your wishes:
Date.LongDate as String Date.LongTime as String Date.ShortDate as String Date.ShortTime as String Date.AbbreviatedDate as String
The following example shows how they are used.
Dim d as new Date() Dim dates() as String d.TotalSeconds = 3204269185 dates.append "TotalSeconds: " + format(d.TotalSeconds, "#") dates.append "Year: " + str(d.Year) dates.append "Month: " + str(d.Month) dates.append "Day: " + str(d.Day) dates.append "Hour: " + str(d.Hour) dates.append "Minute: " + str(d.Minute) dates.append "Second: " + str(d.Second) dates.append "--" dates.append "SQLDateTime: " + d.SQLDateTime dates.append "SQLDate: " + d.SQLDate dates.append "--" dates.append "DayOfWeek: " + str(d.DayOfWeek) dates.append "DayOfYear: " + str(d.DayOfYear) dates.append "WeekOfYear: " + str(d.WeekOfYear) dates.append "--" dates.append "LongDate: " + d.LongDate dates.append "LongTime: " + d.LongTime dates.append "ShortDate: " + d.ShortDate dates.append "ShortTime: " + d.ShortTime dates.append "AbbreviatedDate: " + d.AbbreviatedDate EditField1.text = join(dates, EndOfLine)
I executed this action using three different locale settings (which I changed on my Macintosh in System Preferences, International). The first setting was the standard (for me) setting of United States, using the Gregorian calendar. I followed that by setting my locale to France, and then Pakistan, using the Islamic civil calendar. The only properties that change according to your locale are the LongDate, LongTime, ShortDate, ShortTime, and AbbreviatedDate.
United States, Gregorian Calendar:
TotalSeconds: 3204269185 Year: 2005 Month: 7 Day: 15 Hour: 10 Minute: 46 Second: 25 -- SQLDateTime: 2005-07-15 10:46:25 SQLDate: 2005-07-15 -- DayOfWeek: 6 DayOfYear: 196 WeekOfYear: 29 -- LongDate: Friday, July 15, 2005 LongTime: 10:46:25 AM ShortDate: 7/15/05 ShortTime: 10:46 AM AbbreviatedDate: Jul 15, 2005
France, Gregorian Calendar:
LongDate: vendredi 15 juillet 2005 LongTime: 10:46:25 ShortDate: 15/07/05 ShortTime: 10:46 AbbreviatedDate: 15 juil. 05
Pakistan Islamic Civil Calendar:
LongDate: Friday 8 Jumada II 1426 LongTime: 10:46:25 AM ShortDate: 08/06/26 ShortTime: 10:46 AM AbbreviatedDate: 08-Jumada II-26
The MemoryBlock class is a catch-all class that lets you do a lot of interesting things with REALbasic. MemoryBlocks are generally helpful when dealing with any binary data and can replace some of the missing data types from Visual Basic. They are commonly used with Declares, which are statements in REALbasic that allow you to access functions in shared libraries.
A MemoryBlock is exactly what is says it is: it's a stretch of memory of a certain byte length that you can access and manipulate.
The NewMemoryBlock Function
REALbasic.NewMemoryBlock(size as Integer) as MemoryBlock
There are two ways to instantiate MemoryBlocks in REALbasic. The first is the old-fashioned way, using the New operator, and the second uses the global NewMemoryBlock function.
Dim mb as MemoryBlock Dim mb2 as MemoryBlock mb = New MemoryBlock(4) mb2 = NewMemoryBlock(4)
You may have to look closely to see the difference between the two techniques. The second one is missing a space between New and MemoryBlock because it's a global function and not a Constructor for the MemoryBlock class. Whether you use the traditional way of instantiating an object or the NewMemoryBlock function, you need to pass an integer indicating how many bytes the MemoryBlock should be. The integer can be 0, but some positive number has to be passed to it (a negative number causes it to raise an UnsupportedFormat exception).
In this example, both MemoryBlocks are 4 bytes long. The fact that a MemoryBlock can be of an arbitrary length is what makes it so flexible. It can be anywhere from 0 bytes to however much memory your computer has available (the NewMemoryBlock function returns Nil if there is not enough memory to create the MemoryBlock).
Visual Basic has a Byte data type that takes up 1 byte. This can be replicated with MemoryBlock quite easily:
Dim bytetype as MemoryBlock Dim i as Integer bytetype = NewMemoryBlock(1) bytetype.byte(0) = 1 i = bytetype.byte(0) // i equals 1
MemoryBlock provides a slew of functions to let you get at the individual bytes it contains. I'll review those shortly.
This property refers to the order in which bytes are sequenced when representing a number:
MemoryBlock.LittleEndian as Boolean
I know that's kind of an obtuse sentence, but that's about the best I can do at the moment. As is often the case, an example will be more illuminating. Recall that an integer is made up of 4 bytes. When the computer stores or processes those 4 bytes, it expects them to be in a particular order. This makes perfect sense. The number 41 is not the same as 14, for instance. The problem is that not all operating systems use the same standard. Intel x86 platforms use one type and Macintosh uses another. (With the recent announcement that Macs are moving to Intel chips, this may be less of a problem.) Linux runs on Intel, so you can expect those applications to be the same as those compiled for Windows.
In any event, the example is this. Let's use a really big number, like 10 million. If you look at that number expressed in hexadecimal format on a Macintosh, it looks like this:
00 98 96 80
If you do it on an Intel machine, which is LittleEndian, it looks like this:
80 96 98 00
The terms big-endian and little-endian refer to where the "most significant byte" is in position. In big-endian format, the most significant byte comes first (which also happens to be the way our decimal number system works. In little-endian systems, the least significant byte comes first. According to WikiPedia, this particular nuance can also be referred to as byte order.
You can both read and write to this property. If you are writing a cross-platform application, you need to be aware of this if you will be sharing binary data across systems.
MemoryBlock.Size as Integer
You can read and write to this property. It refers to the size, in bytes, of this MemoryBlock. If you modify the Size, the MemoryBlock will be resized. Bear in mind that if you make it smaller than it currently is, you might lose data.
All five of the following functions return a value of the given type, at the location specified by the Offset parameter:
Function MemoryBlock.BooleanValue(Offset as Integer) as Boolean Function MemoryBlock.ColorValue(Offset as Integer) as Color Function MemoryBlock.DoubleValue(Offset as Integer) as Double Function MemoryBlock.SingleValue(Offset as Integer) as Single Function MemoryBlock.StringValue(Offset as Integer, [Length as Integer]) as String
Because each of these data types has a predefined size (except for string), all you do is pass the Offset to it. If you call MemoryBlock.DoubleValue(0), the MemoryBlock will return a double based on the first 8 bytes of the MemoryBlock.
When using StringValue, you may specify the length of the string, but you do not have to. If you don't, it will just return a string from the offset position to the end of the MemoryBlock. The string can contain nulls and other non-normal characters, so some caution may be necessary when doing this, unless you know for certain what values you will come across.
These three functions work very much like their global function cousins LeftB, MidB, and RightB:
Function MemoryBlock.LeftB(Length as Integer) as MemoryBlock Function MemoryBlock.MidB(Offset as Integer, [Length as Integer]) as MemoryBlock Function MemoryBlock.RightB(Length as Integer) as MemoryBlock
They do the same thing except that instead of returning strings, they return MemoryBlocks.
Get or set a 1-byte integer:
Function MemoryBlock.Byte(Offset as Integer) as Integer Get or set a signed, 2-byte integer:Function MemoryBlock.Short(Offset as Integer) as Integer Get or set an unsigned 2-byte integer.:Function MemoryBlock.UShort(Offset as Integer) as Integer
Get or set a 4-byte integer, just like REALbasic's native type.
Function MemoryBlock.Long(Offset as Integer) as Integer
These four functions, Byte, Short, Ushort, and Long, return integers based on their offset position. They return integers even though they often refer to data that requires fewer bytes to represent, because an integer is really the only thing they can return—it's the smallest numeric data type. The primary use of this is when handling Declares, because it allows you to pass values and access values using a broader selection of data types than those offered by REALbasic.
Also, note that when I say that one of these functions "gets or sets a 2-byte integer," I really mean an integer within the range that can be expressed by 2 bytes. An integer is an integer, and it takes up 4 bytes of memory when REALbasic gets hold of it, even if it can be stored in a MemoryBlock with fewer bytes.
MemoryBlock.Cstring, MemoryBlock.Pstring, MemoryBlock.Wstring, MemoryBlock.Ptr:
Function CString(Offset as Integer) as String
Returns a String terminated.
Function PString(Offset as Integer) as String Function WString(Offset as Integer, [Length as Integer]) as String Function Ptr(Offset as Integer) as MemoryBlock
A pointer to an address in memory. Used by Declares.
Example: Mimicking a Structure with a MemoryBlock
I'd be getting ahead of myself if I gave an example of how to use a MemoryBlock with a Declare, so I've come up with another example. One advantage to using a MemoryBlock (in some cases) is that you can store data in it more efficiently and can often access it faster.
To illustrate this, I've developed two classes, both of which emulate a structure. The first is a class that has only properties, and the second is a subclass of MemoryBlock. These two structurelike classes will hold data representing the two-character United States state abbreviation, the five-digit ZIP code, a three-digit area code, and a seven-digit phone number.
The first class is called Struct, and it has the following properties:
Struct.State as String Struct.Zip as Integer Struct.AreaCode as Integer Struct.PhoneNumber as Integer
If I were to save these items as strings, I would need 17 bytes to represent it—one for each character. The Struct class already reduces that to 14 bytes because it uses Integers for the ZIP code, the area code, and the phone number, all of which can be expressed as Integers.
Implementing the MemoryBlock structure will be a little different because to create the MemoryBlock, I first have to know how many bytes to allocate for it. I could allocate 14 bytes, just like the class version, but I would be allocating more bytes than I need. For example, an area code is three digits, meaning it can be anywhere from 0 to 999. Although an Integer can represent that number, an integer takes 4 bytes and you don't need 4 bytes to represent 999; all you need are 2 bytes. The same is true for the ZIP code. The only number that requires an Integer is the phone number. This means we can shave off 4 bytes and create a MemoryBlock of only 10 bytes. That's a fairly significant size difference.
The next question is how do we best implement the MemoryBlock. I could use the NewMemoryBlock function and pass it a value of 10 to create an appropriate MemoryBlock, but that means I'd have to keep passing 10 to the function each time I wanted to create one and that seems to me to be a bit of a hassle; because I'm a little lazy by nature, I'd like to avoid that if at all possible.
The next option would be to instantiate a New MemoryBlock, but that, too, requires that I pass a parameter to the Constructor and I don't want to do that either. All I want is a class that I can instantiate without having to think about it. The answer is to subclass MemoryBlock and overload the Constructor with a new Constructor that does not take any integers as a parameter. Then, while in the Constructor, call the Constructor of the superclass. Create a subclass of MemoryBlock and call it MemBlockStruct. Create the following Constructor:
Sub MemBlockStruct.Constructor() MemoryBlock.Constructor(10) End Sub
Now you can do this to create a 10-byte MemoryBlock:
Dim mb as MemBlockStruct Dim sz as Integer mb = New MemBlockStruct sz = mb.Size // sz = 10
Because this is a subclass of MemoryBlock, you can still call the original constructor and pass any value you want to it, like so:
Dim mb as MemBlockStruct Dim sz as Integer mb = New MemBlockStruct(25) sz = mb.Size // sz = 25
If you don't want that to happen, you need to override that Constructor and ignore the value that is passed to it:
Sub MemBlockStruct.Constructor(aSize as Integer) Me.Constructor() End Sub
Note that I could have called the superclass MemoryBlock.Constructor(10), like I did in the other Constructor, but that would mean that if at a later time I decided I wanted to add 1 or more bytes to the MemBlockStruct class, I would have to go back and change the value from 10 to something else in both Constructors. This way, should I decide to do that, I need to change only the MemBlockStruct.Constructor() method and no other.
I now have done enough to have two workable classes. Here is how they can be used in practice:
Dim ms as MemBlockStruct Dim ss as Struct Dim a,b,c as Integer ss = new Struct() ss.State = "NH" ss.Zip = 3063 // the first character is "0", which is dropped ss.AreaCode = 603 ss.PhoneNumber = 5551212 ms = new MemBlockStruct() ms.StringValue(0,2) = "NH" ms.Short(2) = 3063 ms.Short(4) = 603 ms.Long(6) = 5551212 a = ms.Short(2) // a = 3063 b = ms.Short(4) // b = 603 c = ms.Long(6) // c = 5551212
Accessing the values of the MemBlockStruct class by the offset position is a little clunky, so I can optionally take the step of creating methods for this class to make it easier. Here is an example for how the methods would look for setting the state abbreviation:
Function MemBlockStruct.setState(aState as String) as String If LenB(aState) = 2 Then me.StringValue(0,2) = aState End If End Function Function MemBlockStruct.getState() as String Return me.StringValue(0,2) End Function
The Collection class is another class, similar to the Dictionary class. The documentation for REALbasic recommends that you use the Dictionary class instead, but that they have retained the collection class for reasons of compatibility with Visual Basic.
The big differences between a Collection and a Dictionary are these:
- Collections can only have strings as their keys.
- A Collection does not use a hash table like a Dictionary does. This means that the time required searching for a value is determined by how many key/value pairs are in the collection. The larger the collection, the longer, on average, it will take to find your value.
Collection uses a slightly different terminology than Dictionaries do. The Add method is roughly equivalent to Value() for Dictionaries, in the sense that you use it to add items to the Collection.
Sub Collection.Add(Value as Variant, Key as String)
Much like it does with a Dictionary, the Count property returns the number of items in the Collection:
Function Collection.Count As Integer
As you would expect, you can get and remove values using the key:
Function Collection.Index(Key as String) as Variant Function Collection.Remove(Key as String) as Variant
You can also get or remove an item by referring to it's indexed position:
Function Collection.Index(Index as Integer) as Variant Function Collection.Remove(Index as Integer) as Variant
When getting or removing an item in a collection using the index, remember that Collection indexes are 1 based, not 0 based like Arrays and Dictionaries.