Data Types and Variables

Last updated Thursday, May 25, 2000

 

Integer values

Visual Basic 5.0 / 6.0

Visual Basic supports three integer types: Byte, Integer and Long.

A variable of type Byte is unsigned, takes up a single byte of memory and can store any integer value between 0 and 255. There is no type declaration character for variables of type Byte.

A variable of type Integer is signed, uses two bytes and can store any value from -32,768 to 32,767. The type declaration character for an Integer is %.

A variable of type Long is signed, is four bytes wide and can store any value from approximately -2 billion to 2 billion. The type declaration character for a Long is &.

These days, most target machines for VB5 and VB6 projects have 32-bit data buses and VB5 and VB6 will only create executables for 32-bit versions of Windows (95, 98 and NT4). Since Long variables are 32 bits wide, it takes less time to perform operations (storage/retrieval or calculations) on Longs than it does for any other variable type. Longs are the preferred type for integers becuase they are faster and have the widest range.

When memory is tight, or perhaps when dealing with a very large array of integer values, you may want to opt for using the Integer or Byte types instead (Integers take up half the space of a Long, Bytes take up half the space of an Integer). The Integer and Byte types are also useful when making calls to API types that require an Integer or Byte-sized parameter.

REALBasic 1.0 / 2.0

REALBasic supports a single integer type: Integer. A variable of type Integer is signed, takes up four bytes and can store any value from approximately -2 billion to 2 billion.

Since Integer variables are 32 bits wide, it takes less time to perform operations (storage/retrieval or calculations) on Integers than it does for any other variable type.

VB to RB

REALBasic's Integer is the equivalent of a Visual Basic Long. Since REALBasic has only one Integer type, variables of type Byte, Integer and Long in VB programs should all be expressed as Integer in RB programs. This should not cause any problems unless your program is dependent on the number of bits used by its variables or if your program contains a large whole numbers stored in an array of type Integer or Byte rather than Long to save memory.

type storage required range default value
VB Byte 1 byte 0 to 255 0
VB Integer 2 bytes -32,768 to 32,767 0
VB Long 4 bytes -2,147,483,648 to 2,147,483,647 0
RB Integer 4 bytes -2,147,483,648 to 2,147,483,647 0

 

Floating-point values

Visual Basic 5.0 / 6.0 and REALBasic 1.0 / 2.0

Both Visual Basic and REALBasic make us of Single and Double variable types.

A variable of type Single is signed, takes up four bytes of memory, and can represent absolute values as small as approximately 1.4E-45 to as large as 3.4E38 in both positive and negative directions. Singles can hold up to 38 digits, but are accurate only to the first 7. Both VB and RB Singles use the IEEE standard for single precision floating point values in their internal representation.

In VB, the type declaration character for a Single is !.

A variable of type Double is signed, takes up eight bytes of memory, and can represent absolute values as small as approximately 4.9E-324 to as large as 1.8E308 in both positive and negative directions. Doubles can hold up to 300 digits, but are accurate only to the first 16. Both VB and RB Doubles use the IEEE standard for double-precision floating point values in their internal representation.

In VB, the type declaration character for a Double is #.

type storage required range default value
VB/RB Single 4 bytes

Negative values:

-3.402823E38 to -1.401298E-45

Positive values:

1.401298E-45 to 3.402823E38

0
VB/RB Double 8 bytes

Negative values:

-1.79769313486232E308 to -4.94065645841247E-324

Positive values:

4.94065645841247E-324 to 1.79769313486232E308

0

 

IEEE standard for floating point values

Most programming languages' floating point arithmetic conforms to the IEEE standard. Here are some sites that cover the standard:

The Pittsburgh Supercomputing Center's page on IEEE floating-point arithmetic

Linux Support Line's notes on IEEE math

A good writeup on floating-point numbers that first covers the basic before covering the IEEE standard

JavaWorld's writeup on IEEE floating-point values and how they apply to Java

 

Strings

Visual Basic 5.0 / 6.0

Visual Basic supports two String types: Variable-length and Fixed-length.

The default type of String is the variable-length string. A variable-length string changes its size dynamically to store as many characters as required, up to a maximum of approximately 2 billion characters (the documentation doesn't state a specific number, but a reasonable guess would be 2,147,483,647, which is the number of addresses that can be accessed with a signed long pointer variable).

Fixed-length strings were often used in earlier VB applications when memory and disk space were not as cheap as they currently are. VB and VBA in a Nutshell says they are now rarely used, but Michael Loader reminded me that they're still used when making API calls.

As the name suggests, fixed-length strings do not vary in size but instead have their sizes specified when they are declared. The range of allowed sizes for fixed-length strings is 1 to 65.400 characters. To specify as fixed-length string, simply follow the keyword String with the multiplication sign followed by the desired length. For example, the following code declares a fixed length string 20 characters long:

Dim fixedLengthString as String * 20

The unused characters in a fixed-length string are occupied by space characters (ASCII 32).

The type declaration character for Strings is $.

Assigning values to and reading values from fixed strings is faster than for variable-length strings. According to an article in the January 1999 edition of Visual Basic Programmer's Journal, the process of creating, filling and destroying an array of 100,000 strings is 50 times faster when using of 20-character fixed strings as opposed to variable-length strings. According to the article, fixed-length strings are stored as a continuous memory block and allocated in a single fast operation.

However, string operations other than assignment or reading are slower with fixed-length string than with variable-length strings. Internally, string functions in VBA (Visual Basic for Applications, the core of VB) operate only on variable length strings; according to the article mentioned above, "VB has to convert [fixed-length strings] temporarily to variable-length strings".

Visual Basic has a very complete set of string functions, which have been expanded in VB6, where many string functions from VBScript have been incorporated into the language.

REALBasic 1.0 / 2.0

REALBasic supports a single String type. As with Visual Basic's variable-length strings, they expand and contract as needed to accomodate the strings they are assigned. According to the RB documentation, RB strings are limited in size only by available memory.

VB to RB

RB does not have fixed-length strings, so you'll have to remove the length specification from any string declarations. Here's a fixed-length string declaration in VB:

Dim theString as String * 20

Here's the conversion to RB:

Dim theString as String

As mentioned in the VB section, any unused characters positions in a fixed-length string are occupied by spaces. If your VB program logic "expects" strings set up in this way and if you want to preserve this logic, you will have to initialize your strings with the appropriate number of spaces. When changing the contents of such strings in RB, make sure that the changes leave them at a constant length, padding unused character positions with spaces.

type storage required range default value
VB String, variable length 10 bytes + length of string

0 to approximately 2 billion characters

(exact number believed to be 2,147,483,647)

""

(zero-length string)

VB String, fixed length

length of string 0 to 65,400 characters a number of spaces equal to the fixed length of the string
RB String unknown; probably some small constant under 16 bytes + length of string limited only by memory allocated to program

""

(zero-length string)

 

Booleans

Visual Basic 5.0 / 6.0

A variable of type Boolean takes up two bytes of memory and can take on one of two values: True or False.

While it is obvious that the result of a an assignment of True or False to a boolean variable and the result of a Boolean function is a boolean value, it is not obvious to many people that the result of a comparison operation (=, <, >, <=, >=) is also a Boolean.

In VB, there are functions for converting Booleans to numerical values and vice versa. When converting from booleans to numbers, True maps to -1 and False maps to 0. When converting from numbers to booleans, any non-zero value maps to True, while 0 maps to False.

type storage required range default value
VB Boolean 2 bytes

True or False

False

RB Boolean

Unknown True or False False

 

 

Objects

Visual Basic 5.0 / 6.0 REALBasic 1.0 / 2.0

Both Visual Basic and REALBasic use a Java-like system for variables that refer to objects. Rather than containing the object itself, object variables in VB and RB contain a reference to the object. Another similarity that both languages have to Java is the use of the New keyword create instances of a class.

When assigning an object reference to a variable, there is a slight difference in syntax between VB and RB. While RB lets you assign obejct references as if they were any other kind of variable, VB specifically requires the use of the Set keyword for object assignments.

The following VB code creates a new instance of a class called MyClass and stores a reference to this instance in the variable myInstance:

Set myInstance = New MyClass

The Set keyword denotes that the variable myInstance is meant to receive a reference to an object.

Here is the same code in RB:

myInstance = New MyClass

Note that there is no need for the keyword Set in RB. This is closer to Java's object model, which appears to have been used as inspiration for the RB object model.

Both VB and RB have a generic called object type called Object, to which instances of any class (and in VB's case, any COM-based object, which includes things such as ActiveX objects) can be assigned. In VB, the Object type can be considered to be a "Variant for objects"; a reference to any type of object can be stored in variables of this type. The RB Object type is more in line with the Java model; Object is the base class for all objects in RB, whether intrinsic or created by the user.

In VB, variables of type Object are late-bound.

VB/RB object variables hold object references

If you know C++ or Java, here's something that should give you an idea of how object variables in VB and RB work.

The following declaration in VB and RB...

Dim myInstance as MyClass

...is analogous to the following declaration in Java...

MyClass myInstance;

...and is analogous to the following declaration in C++...

MyClass* myInstance;

This has consequences for the = operator when it comes to objects. Consider the following code in RB:

Dim myInstance1 as MyClass

Dim myInstance2 as MyClass

myInstance1 = new MyClass

myInstance2 = myInstance1

In the third line of code, a new instance of MyClass is created and a reference to that new instance is stored in myInstance1. In the fourth line of code, the reference stored in myInstance1 is copied to myInstance2. Note that the reference to the instance -- not the instance itself -- is being copied. myInstance1 and myInstance2 are referencing the same instance. This storage of object references in object variables instead of object instances is like the system used by Java.

Objects will be covered in-depth on the OOP page of this site.

type storage required range default value
VB Object 4 bytes

any object reference

Nothing
RB Object unknown any object reference nil

 

Dates and Times

Visual Basic 5.0 / 6.0

VB represents dates and times using the Date type. A variable of type Date is unsigned, takes up eight bytes of memory and can store any date from 00:00:00 1 January 100 to 23:59:59 31 December 9999.

Date literals are indicated by expressing the date and time in any one of several accepted formats, with the entire expression bracketed by # symbols. For example, the following to set a Date variable called theDate to 19:34:56 5 July 1998, you could use any one of the following lines:

theDate = #19:34:56 September 05, 1998#

theDate = #09/05/1998 7:34:56pm#

theDate = #5 Oct 1998 07:34:56p#

There are still more ways the date could be expressed. The key is to make sure that you clearly specify a day, month, year and time.

A Date value specifies both date and time. The default value for Date is 00:00:00 30 December 1899. If you specify a date but not a time, the default time of 00:00:00 is used. If you specify a time, but not a date, the default date of 30 December 1899 is used.

Internally, VB makes use of a Double to store the date value, with each day being equivalent to 1.0, and parts of a day being expressed in terms of fractions. This setup makes date arithmetic possible. For example, if you had a Date value stored in the variable d, you could change its value to exactly one week later simply by adding 7 to d. If you wanted to know the time interval between two dates down to the second, date1 and date2, date2 being the later date, you carry out the subtraction operation d2-d1 (the DateDiff function allows you to determine any specified time interval, such as weeks, days or months, between any 2 dates). You can also use the comparison operators (=, <, >, <=, >=) to determine which of two date/time values is the earlier or later one. The value of 0.0 maps to 00:00:00 30 December 1899. The earliest date and time in the Date range, 00:00:00 1 January 100 maps to the Double value -657434.0. The latest date and time in the Date range, 23:59:59 31 December 9999 maps to the Double value 2958465.99998843.

The chart below shows the Double equivalents of time periods of one day and smaller.

time interval Double equivalent
1 second 1/86400, which VB represents as 1.15740740740736E-05
1 minute 1/ 1440, which VB represents as 6.94444444444449E-04
1 hour 1 / 24, which VB represents as 4.16666666666667E-02
1 day 1.0

REALBasic 1.0 / 2.0

Rather than use a simple data type for dates, RB uses Date objects to represent dates. I don't have any information on how many bytes are used up by a Date object, and from experimenting with different date values and comparing them to VB's results, its range is at least from 00:00:00 1 January 100 to 23:59:59 31 December 9999. The default date of a Date object is the date and time at which the object was instantiated.

As with any object, the values associated with a Date object are set and read through its properties. The following chart lists the properties of the Date class:

Property Type Changeable at run-time? Description
AbbreviatedDate String no

Returns a string spelling out the date using abbreviated day and month names, for example:

Wed, Dec 31, 1997

Day Integer yes The date number. For example, setting this value to 25 specifies the 25th day of the month.
DayOfWeek Integer no

A number corresponding to the day of the week:

  • Sunday = 1
  • Monday = 2
  • Tuesday = 3
  • Wednesday = 4
  • Thursday = 5
  • Friday = 6
  • Saturday = 7
DayOfYear Integer yes A number corresponding to the day of the year. For example, setting this value to 200 specifies the 200th day of the year.
Hour Integer yes

The hour of the day, from 0 to 23.

 

LongDate String no

Returns a string spelling out the date using full day and month names, for example:

Wednesday, December 31, 1997

LongTime String no

Returns a string spelling out the time using hours:minutes:seconds AM/PM format. For example:

2:32:40 PM

Minute Integer yes The minute of the hour, from 0 to 59.
Month Integer yes The month of the year, from 1 to 12.
Second Integer yes The second of the minute, from 0 to 59.
ShortDate String no

Returns a string spelling out the date using numeric MM/DD/YY format, for example:

12/31/97

ShortTime String no

Returns a string spelling out the time using hours:minutes AM/PM format. For example:

2:32 PM

TotalSeconds Integer yes Number of seconds elapsed since 00:00:00 1 January 1904.
WeekOfYear Integer no The week of the year, from 1 to 52.
Year Integer yes The year. Seems to work properly for the years 100 through 9999.

VB to RB

The setting of Date values is quite different between VB and RB. While not difficult, it involves a fair bit of retyping. Let's start with a simple date-only example. Let's say we want to assign the date 5 July 1998 to the variable theDate. The VB code is:

dim theDate as Date

theDate = #7/5/1998#

The equivalent code in RB is:

dim theDate as Date

theDate = new date

theDate.day = 5

theDate.month = 7

theDate.year = 1998

Another example -- suppose we want to assign the date of 12:00:00 5 July 1998 to the variable theDate. The code in VB is:

dim theDate as Date

theDate = CDate("12:00:00 5 July 1998")

Here's the equivalent code in RB:

dim theDate as Date

theDate = new date

theDate.hour = 12

theDate.minute = 0

theDate.second = 0

theDate.day = 5

theDate.month = 7

theDate.year = 1998

Multiple Properties and the With Structure

This would be less painful if RB had a With structure like VB's or Pascal's, which makes it possible to set several properties of an object while only specifying the object's name once. If RB had such a structure, the above code would simplify to:

Dim theDate as Date

theDate = New date

With theDate

.hour = 12

.minute = 0

.second = 0

.day = 5

.month = 7

.year = 1998

End With

As if settings dates in RB as opposed to VB weren't annoying enough, performing date arithmetic is even worse. As of the current version, the Date class provides no date arithmetic methods (in fact, there are no methods listed). You can find the difference between two dates by performing subtractions using the Year and DayOfYear properties; make sure you account for leap years. Beyond that, you'll have to do some serious coding to convert any VB date arithmetic to RB. I do have one suggestion -- to make your life easier, I would recommend writing a routine that converts a Date object's properties into a Double, using the same date representation as VB.You might even want to subclass the Date class and add this method, along with others to that new class. I'm going to spend the next little while expanding this site, but expect to show the code for such a class in a few weeks.

type storage required range default value
VB Date 8 bytes 1 January 100 to 31 December 9999 00:00:00 30 December 1899
RB Date object unknown believed to be 1 January 100 to 31 December 9999 date and time of the object's instantiation

 

Variants

Visual Basic 5.0 / 6.0

The Variant data type is "universal type" that can hold most or all other data types. In VB5, variants can hold any data type except for user-defined types, while variants in VB6 can hold any data type. Variants take up a minimum of 16 bytes of memory and are structured as shown below:

Type info - 2 bytes

This section describes the data contained within the variant. You can see this value by using the VarType function.

Reserved - 8 bytes

As of VB6, these appear to have no purpose other than to pad out the length of a variant to 16 bytes.

Data - 8 bytes

If the value being stored in the variant fits in 8 bytes or less, it is stored in this data section. If the value is larger than 8 bytes, this section holds a pointer to the actual data.

The following chart shows lists the type number stored in the type info section of a variant data structure as well as corresponding conversion and test functions.

Type Type Number Conversion Function Test Function
Empty 0 = Empty IsEmpty
Null 1 = Null IsNull
Integer 2 CInt IsNumeric
Long 3 CLng IsNumeric
Single 4 CSng IsNumeric
Double 5 CDbl IsNumeric
Currency 6 CCur IsNumeric
Date 7 CDate / CVDate IsDate
String 8 CStr  
Object 9   IsObject
Error 10 CVErr IsError
Boolean 11 CBool  
Variant 12 CVar  
Data Object 13    
Decimal 14 CDec IsNumeric
Byte 17 CByte  
User-Defined Type 36    
Array 8192 Array IsArray

For example, suppose we have declare a variant myVariant and set its contents to the string "Hello". The type number returned by the VarType function would be 8 (see the chart above) and the function IsString(myVariant) would return TRUE.

The major price of a variant's versatility is speed. With an integer variable, VB can access the value directly. With a variant containing an integer, VB must first perform an internal variant-to-integer conversion before it can work with the integer value. Obviously, there will be a slight delay involved, and if the operation is performed within a loop, the delay could be significant. Since variants always take a minimum of 16 bytes, they also take up more space than many data types. For these reasons, most articles and books on VB suggest that programmers avoid use of the variant data type whenever necessary.

Chapter 4 of Advanced Visual Basic puts forth an alternative argument, suggesting that variants should be used all the time and specific data types should be used only when necessary. It states that the slowdown involved in using variants is really insignificant compared to other operations (form drawing, calling databases, user response time), the extra size of a variant is insignificant in most cases, use of a flexible data type will reduce run-time type mismatch errors and the flexibility of a variant makes code easier to maintain.

REALBasic 1.0

No equivalent in REALBasic 1.0

REALBasic 2.0

REALBasic 2.0 has a variant type. Details to follow shortly

type storage required range default value
VB Variant 16 bytes

The combined ranges of all variable types

Empty
RB Variant unknown The combined ranges of all variable types unknown

 

Currency

Visual Basic 5.0 / 6.0

A variable of type Currency is signed, takes up a eight bytes of memory and can represent values from approcimately -9.2 trillion to 9.2 trillion. It is designed to hold up to 19 digits, has 19-digit accuracy and has a maximum of 4 decimal places. The type declaration character for Currency is @.

As its name suggests, Currency is meant to be used for storing monetary values and is also the the Visual Basic built-in data type with the highest accuracy.

The Currency type is represented internally as a 19-digit integer which is then scaled by 10,000, which yields the four decimal places. According to Core Visual Basic 5, this type is designed to avoid the problems inherent in coverting certain fractions from their internal binary representation to their decimal representations (the example the book uses this example: it's difficult to express 1/10 as any combination of bnary fractions -- 1/2, 1/4, 1/8, 1/16 and so on). The book doesn't go any further than this explanation, but I suspect that this type makes use of binary coded decimal (BCD) representation. In BCD, rather than representing decimal numbers in their direct binary equivalents, each digit is represented by a four-bit binary number. This digit-by-digit representation is slower for calculations, but avoids the loss of accuracy encountered when converting between binary and decimal.

REALBasic 1.0 / 2.0

There is no equivalent to VB's Currency type. There are two possible approaches to conversion. The RB type that corresponds most closely to Currency is Double, although it's neither fixed-point nor does was it designed to make binary-to-decimal fraction conversions more accurate.

type storage required range default value
VB Currency 4 bytes

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

0

 

Records

Visual Basic 5.0 / 6.0

VB's user-defined type (UDT) is a structured data type similar to Pascal's record or C/C++'s struct. They are defined as follows:

Type TypeName

variable1 as varType1

variable2 as varType2

...

variableN as varTypeN

End Type

After being defined, you can declare variables of that user-defined type. The component variables (often called fields) of a UDT are accessed using "dot notation." See the example below:

Type Person

name as String

age as Long

End Type

...

Dim me as Person

me.name = "Joey"

me.age = 31

REALBasic 1.0 / 2.0

No equivalent.

 

Bibliography/Acknowledgements

VB and VBA in a Nutshell: The Language. Paul Lomax, O'Reilly and Associates 1998.