Home > Articles

Classes and Objects

  • Print
  • + Share This
This chapter is from the book

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:

property.color=blue
property.size=1

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.

Dictionary Properties

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

Dictionary Methods

The following method removes all the key/value pairs from the Dictionary:

Sub Dictionary.Clear

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
  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020