1. Technology

Data types in VB.NET

Understanding the VB.NET Data Type


Businessman Typing
UFO RF/a.collectionRF/Getty Images

You may be wondering whether VB.NET data types are really that important. If you are, consider this quote from the O'Reilly book by Steven Roman, Ph.D., Ron Petrusha, and Paul Lomax, "VB .NET Language in a Nutshell" (ISBN: 0596003080)"

The most important change (from VB 6 to VB.NET) is that all of the languages under the .NET umbrella ... now implement a subset of a common set of data types, defined in the .NET Framework's Base Class Library (BCL).

"The most important change in .NET." Does it sounds more like something you ought to know about now?

Because all .NET languages share a consistent and structured way to represent data, bugs and security holes due to the way information is stored in a program (see my article about the Decimal data type) are a thing of the past. And in addition, every .NET language can write, read, and share data without problems. For example, one of the biggest reasons VB 6 has a hard time working alongside C++ is that VB 6 simply can't duplicate the C++ pointer. That doesn't happen in .NET.

The first word to understand is "type". This word is used a lot in .NET and it can be confusing to new students of VB.NET. But the actual meaning is very simple. A type is just a .NET class. People tend to use the word, "type" when they're talking about a fundamental, predefined part of .NET and "class" when they're talking about part of a program, but the two terms can be interchanged most of the time.

Since a type is a class, it can have all the things that classes have: methods, properties, and so forth. We'll return to this point just a little later.

A "data type" is a class that is primarily used just to hold data. This is different from most other classes since they're primarily intended to 'do something', like access a database or or format a page for example. Although data types can "do something" as well (that is, they also have methods), this isn't their main purpose.

The list below shows the data types that are defined in the .NET System namespace.

Boolean - true or false

Byte - 8 bit unsigned integer

Char - 16 bit Unicode character

DateTime - date and time of day

Decimal - decimal number

Double - 64 bit floating-point number

Int16 - 16 bit signed integer

Int32 - 32 bit signed integer

Int64 - 64 bit signed integer

SByte - 8 bit signed integer

Single - 32 bit floating-point number

UInt16 - 16 bit unsigned integer

UInt32 - 32 bit unsigned integer

UInt64 - 64-bit unsigned integer

You probably recognize these as just the way you declare a variable in VB.NET:

Dim myVar as <data type goes here>

You can view these together in the VB.NET Object Browser by selecting "Group by Object Type" from the context menu (right click in the Object Browser window). The illustration shows all of the "intrinsic" data types.

Click Here to display the illustration
Click the Back button on your browser to return

The one "intrinsic" that is in the illustration, but not in the list above is the Object object. The Microsoft .NET documentation of this object tells it all:

Object supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

Everything inherits from Object - everything.

Object has seven methods:

Equals(Object) as Boolean

Equals(Object, Object) as Boolean

GetHashCode() as Integer

GetType() as System.Type


ReferenceEquals(Object, Object) as Boolean

ToString() as String

If you look at another data type such as ... just to pick one more or less at random ... Char in the VB.NET Object Browser, you'll see six of the seven methods are also available. The missing one is the constructor method, New(). This is taken care of when a variable is declared. But you'll also see a lot of additional methods. For example, in VB 6, you frequently find code like this ...

If (UCase(chrl) >= "A" And UCase(chrl) <= "Z") Then ...

... to find out if a one character string is actually a letter.

In VB.NET, the Char data type has a method for that:

If Char.IsLetter(myCharVar) Then ...

The bottom line here is that data types make VB.NET better but they also make your programming easier.

  1. About.com
  2. Technology
  3. Visual Basic
  4. The .NET Framework
  5. Understanding the VB.NET Data Types

©2014 About.com. All rights reserved.