- 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
Revisiting the StringParser Module
In the previous chapter, I created a StringParser module that did two things: it would take a string and split it up into sentences and it would take a string and split it up into words. Although a module works just fine, I think it would be worthwhile to take that same code and rethink it in terms of classes and objects.
Turning the module into a class is fairly straightforward. You can follow the same original steps to get started; the only difference is that you should click New Class rather than New Module in the editor and name it Parser. The differences come into play with the methods. Because one of the goals of object-oriented design is to maximize code reuse, it's instructive to think about this problem in terms of code reuse. You also want to think about it in terms of making it easy to work with. With the module, we had two different methods we could call, and they both called the same private method.
Right away, this creates an opportunity because the two methods are already sharing code—the private Split method.
Because I want to maximize code reuse, I'll dispense with the constant and use properties instead. The first step is to create a string property that will hold the string that will be used to split up the other string. There's no need for any other object to access this property directly, so I'll set its scope to Protected. If I set the scope to Private, subclasses of the base class will not be able to access it, and I want them to be able to do that.
Parser.Delimiter as String
In the module, the two primary functions returned an Array. I could do the same with this class, but that's actually a little more awkward because when I call those functions, I have to declare an Array to assign the value that's returned from them. If I were to do the same with a class, I would have to declare the Array plus the class instance, which strikes me as extra work. Instead, I can create the Array as a property of the class. You create an Array property just like any other data type, except that you follow the name with two parentheses. I'll call this Array Tokens.
Parser.Tokens() as String
Next I will implement the protected Split method, but with a few differences. The biggest is that I do not need to return the Array in a function. Instead, I am going to use the Tokens() Array, which is a property of the class. First, this means that the function will now be a subroutine and instead of declaring a word_buffer() Array, I'll refer to the Tokens() Array.
Protected Sub Split(aString as String) Dim chars(-1) as String Dim char_buffer(-1) as String Dim x,y as Integer Dim pos as Integer Dim prev as Integer Dim tmp as String // Use the complete name for the global Split function // to avoid naming conflicts chars = REALbasic.Split(aString, "") y = ubound(chars) prev = 0 for x = 0 to y pos = me.Delimiter.inStr(chars(x)) // If inStr returns a value greater than 0, // then this character is a whitespace if pos > 0 then Me.Tokens.append(join(char_buffer,"")) prev = x+1 reDim char_buffer(-1) else char_buffer.append(chars(x)) end if next // get the final word Me.Tokens.append(join(char_buffer,"")) End Sub
One thing you may have noticed is the use of the word Me when referring to Tokens. That's because Tokens is a property of the Parser class and, as I said before, you refer to an object by the object's name and the name of the property or method you are wanting to access. REALbasic uses the word Me to refer to the parent object where this method is running, and this helps to distinguish Tokens() as a property rather than as a local variable. REALbasic 2005 is much more finicky about this than previous versions (and rightly so, I think). Before, Me was mostly optional, but now it is required. We'll revisit Me when talking about Windows and Controls, because there is a companion to Me, called Self, that comes into play in some situations.
I also pass only the string to be parsed as a parameter, because the delimiter list value will come from the Delimiter property.
Now, the final two questions are how to set the value for the Delimiter property and how to replicate what was done previously with two different methods. I'm going to do this by subclassing Parse, rather than implementing them directly.
Create a new class and call it SentenceParser and set the Super to be Parser. Create a new Constructor that doesn't take any parameters, like so:
Sub Constructor() me.Delimiter = ".,?!()" End Sub
Create another method called Parse:
Sub Parse(aString as String) me.Split(aString) End Sub
Now if I want to parse a string into sentences, I do this:
Dim sp as SentenceParser Dim s as String sp = new SentenceParser() sp.Parse("This is the first sentence. This is the second.") s = sp.Tokens(0) // s= "This is the first sentence"
If I want to add the functionality of splitting the string into words, I need to create a new class, called WordParser, set the Super to SentenceParser, and override the Constructor:
Sub Constructor() me.Delimiter = " " + chr(13) + chr(10) End Sub
And I can use this in this manner:
Dim wp as WordParser Dim s as String wp = new WordParser() wp.Parse("This is the first sentence. This is the second.") s = wp.Tokens(0) // s= "This" s = wp.Tokens(Ubound(wp.Tokens)) // s = "second."
In this particular example, I think a strong argument could be made that it would have been just as easy to have stuck with the module. That's probably true, because this is a simple class with simple functionality. You do have the advantage, however, of having an easy way to add functionality by subclassing these classes, and each one uses the same basic interface, so it's relatively easy to remember how to use it.
REALbasic provides a large number of predefined classes. Most of the rest of this book will be examining them. In the next section, I'll take a look at one in particular, the Dictionary class, and then I'll show a more realistic (and hopefully useful) example of how you can subclass the Dictionary class to parse a file of a different format.
The Dictionary Class
Although you will be creating your own classes, REALbasic also provides you with a large library of classes that you will use in your applications. You've been exposed to some of them already—Windows and Controls, for example.
The Dictionary class is a class provided by REALbasic that you'll use a lot, and it will serve as the superclass of our new Properties class.
A Dictionary is a class that contains a series of key/value pairs. It's called a Dictionary because it works a lot like a print dictionary works. If you're going to look up the meaning of a word in a dictionary, you first have to find the word itself. After you've found the word you can read the definition that's associated with it.
If you were to start from scratch to write your own program to do the same thing, you might be tempted to use an Array. To implement an Array that associated a key with a value, you would need to create a two-dimensional Array something like this:
Dim aDictArray(10,1) as String aDictArray(0,0) = "My First Key" aDictArray(0,1) = "My First Value" aDictArray(1,0) = "My Second Key" aDictArray(1,1) = "My Second Value" aDictArray(2,0) = "My Third Key" aDictArray(2,1) = "My Third Value" // etc...
If you wanted to find the value associated with the key "My Second Key", you would need to loop through the AArray until you found the matching key.
For x = 0 to 10 If aDictArray(x, 0) = "My Second Key" Then // The value is aDictArray(x,1) Exit End If Next
Using this technique, you have to check every key until you find the matching key before you can find the associated value. This is okay if you have a small list, but it can get a little time consuming for larger lists of values. One feature of a printed dictionary that makes the search handy is that the words are all alphabetized. That means that when you go to look up a word, you don't have to start from the first page and read every word until you find the right one. If the keys in the Array are alphabetized, the search can be sped up, too.
Using the current Array, the best way to do this would be with a binary search, which isn't a whole lot different from the way you naturally find a word in the dictionary. For example, if the word you want to look up starts with an "m," you might turn to the middle of the dictionary and start your search for the word there. A binary search is actually a little more primitive than that, but it's a similar approach. To execute a binary search for "My Second Key", a binary search starts by getting the upper bound of the Array ("10", in this case) and cutting that in half. This is the position in the Array that is checked first .
result = StrComp(aDictArray(5,0), "My Second Key")
If the result of StrComp is 0, you've found the key. However, if the value returned is -1, then "My Second Key" is less than the value found at aDictArray(5,0). Because the key of aDictArray(5,0) would be "My Sixth Key", -1 is the answer we would get. So the next step is to take the value 5 and cut it in half, as well (I'll drop the remainder). This gives us 2. Now you test against aDictArray(2,0) and you still get -1, so you cut 2 in half, which gives you 1. When you test aDictArray(1,0), you find that it matches "My Second Key", so you can now find the value associated with it.
This is all well and good if you happen to have a sorted two-dimensional Array consisting of one of REALbasic's intrinsic data types. If the Array isn't sorted (and you have to jump through some hoops to get a sorted two-dimensional Array), things begin to get complicated. What happens if the key is not an intrinsic data type?
These are really the two reasons for using Dictionaries—you don't have to have a sorted list of keys to search on, and your keys do not have to be limited to intrinsic data types. It's still easier (and possibly more efficient) to sequentially search through an Array if the list of keys is relatively short. As the list grows larger, the more beneficial the use of a dictionary becomes.
The Properties File Format
For our new subclass, we are going to be parsing a properties file. Java developers often use properties files in their applications. The file format used by properties files is very simple, so I will use this format in my example.
The format is a list of properties and an associated value separated (or delimited) by an equal sign (=). In practice, the property names are usually written using dot notation, even though that's not necessary. Here's an example:
The format doesn't distinguish between integers and strings or other data types, so this unknown is something that the module will need to be prepared to deal with. Because the properties file has a series of key/value pairs, a Dictionary is well suited to be the base class. The Dictionary already has members suited for dealing with data that comes in a key/value pair format, so our subclass will be able to use those properties and methods, while adding only a few to deal with the mechanics of turning a string in the properties file format into keys and values in the Dictionary.
Dictionaries use hash tables to make searches for keys faster. Without getting into too much detail, you can adjust the following property at times in order to optimize performance:
Dictionary.BinCount as Integer
In most cases you'll never need to adjust this because the class automatically adjusts it as necessary, but there are times when you can improve performance. This is especially true if you know you are going to have a particularly large dictionary with lots of key/value pairs.
The following property returns the number of key/value pairs in the dictionary:
Dictionary.Count as Integer
The following method removes all the key/value pairs from the Dictionary:
This removes a particular entry, based on the key:
Sub Dictionary.Remove(aKey as Variant)
When using a Dictionary, you almost always access the value by way of the key. That's more or less the whole point of a Dictionary. You do not always know if any given key is available in the Dictionary, so you need to find out if the key exists first before you use it to get the associated value. Use the following method to do so:
Function Dictionary.HasKey(aKey as Variant) as Boolean
The values in a Dictionary are accessed through the Value function.
Function Dictionary.Value(aKey as Variant) as Variant
Here's an example of how to use the Value function:
Dim d as Dictionary Dim s as String d = new Dictionary d.value("My key") = "My value" If d.HasKey("My Key") Then s = d.value("My key") // s equals "My value" End If
The Dictionary class also provides you with a way to get access to the keys by their index, which can be helpful in some circumstances, using the following method:
Function Dictionary.Key(anIndex as Integer) as Variant
There's no guarantee that the keys are in any particular order, but this can be used in circumstances where you want to get all the key/value pairs. Assume d is a Dictionary with 100 key/value pairs:
Dim d as Dictionary Dim x as Integer Dim ThisKey, ThisValue as Variant Dim s as String // Assign 100 haves to d... For x = 0 to 99 // ThisKey = d.Key(x) ThisValue = d.Value(ThisKey) s = s + ThisKey.StringValue + ":" + ThisValue.StringValue + Chr(13) Next
This example takes all the keys and values in the Dictionary and places each key/value pair on a single line, separated by a colon. The ASCII value for a newline character is 13, which explains the Chr(13).
The Lookup function is new for REALbasic 2005:
Function Dictionary.Lookup(aKey as Variant, defaultValue as Variant) as Variant
It works like the Value function, but instead of passing only the key, you also pass a default value to be used if the key doesn't exist in the dictionary. This saves you the nearly unbearable hassle of having to use the HasKey() function every time you try to get at a value in the Dictionary.
Here's how it works:
Dim d as Dictionary Dim s as String d = New Dictionary() d.Value("Alpha") = "a" d.Value("Beta") = "b" s = d.Lookup("Delta", "d") // s equals "d"
In this example, the key "Delta" does not exist, so d returns the default value "d" and this value gets assigned to s.
This next example can be used in replacement of this older idiom, which is the way you had to do it before the Lookup method was added:
Dim d as Dictionary Dim s as String d = New Dictionary() d.Value("Alpha") = "a" d.Value("Beta") = "b" If d.HasKey("Delta") Then s = d.Value("Delta") Else s = "d" End If