Home > Articles > Software Development & Management > Object Technology

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

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 Class

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.

ParseDate Function

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 Jan. 2006

Date Properties

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

MemoryBlock Class

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.

MemoryBlock Properties

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.

MemoryBlock Methods

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()
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)
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

Collection Class

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 Methods

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.

  • + 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.


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.


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.


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.


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


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


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.


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.


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