VB.NET Data Types

Understanding data and how it is represented is key to
making use of it in a program. In this segment we’ll walk thru VB.NETs standard
data types and talk about how to make use of them. We’ll also look at some of
the .NET runtime utilities that make it possible to change data from one type
to another. When you see the word "type" in relation to VB.NET, it
essentially means a .NET
class. A "data type," then, is a class used to hold or represent
different data values.

You also need to have some understanding of number systems
and how a computer stores information. The most basic storage element in a
computer is the bit. A bit is either a one (1) or a zero (0). If you group
eight bits together, you get a byte. You’ll see byte used when describing how
much memory a computer has, usually in either Mega (MB) or Giga (GB) bytes. The
byte is also the smallest unit of memory most computers are able to address.
All other data types are typically expressed in the number of bytes they
require for a single value. Table 1 shows the VB.NET data types and how many
bytes are required for each.


 

 

 

Data Type

# of Bytes

Values

Boolean

1

True or False

Byte

1

Unsigned

Char

2

Unicode character

Single

4

32-bit Floating Point Number

Double

8

64-bit Floating Point Number

Integer

4

32-bit Signed Integer

Int16

2

16-bit Signed Integer

Int32

4

32-bit Signed Integer

Int64

8

64-bit Signed Integer

Long

8

64-bit Signed Integer

SByte

1

8-bit Signed Integer

Short

2

16-bit Signed Integer

UShort

2

16-bit Unsigned Integer

UInt16

2

16-bit Unsigned Integer

UInt32

4

32-bit Unsigned Integer

UInt64

8

64-bit Unsigned Integer

DateTime

8

Date and time of day

Decimal

16

Decimal number

Table 1 VB.NET Data Types

Strings are probably the most common data type in terms of
usage in programs. Representing a string in memory differs from one language to
another. Many legacy languages represent a string in memory as an undetermined
number of bytes terminated by the null character (0). VB.NET actually uses a
length field to set how long the string is and does not null terminate the
string. This can cause issues when passing strings between different languages
if a null-terminated value is expected. VB.NET strings can also contain null
characters, so this could also cause problems if you’re passing that value to
some other routine.

VB.NET uses the Dim reserved word to define a variable with
a specific type. So, to define the variable I as an Integer you would type:

Dim I as Integer

You can also assign an initial value to a variable using the
Dim statement. Here’s how you would declare a variable named myPi as a Double
and assign it the value of 3.1415:

Dim myPi as Double = "3.1415"

The .NET runtime includes a wide range of functions and
methods for creating and manipulating strings. If you have a need to build
strings from multiple pieces, you’ll want to take a look at the StringBuilder
class. It has been highly optimized to perform the most common string
operations as quickly and efficiently as possible. To use this class, simply
declare a variable as follows:

Dim myNewString as New System.Text.StringBuilder()

Once you have the variable declared, you’ll have access to
Intellisense within Visual
Studio
to show you the different methods you have access to (see Figure
1). We’ll make use of this and other .NET data manipulation in future articles.

Visual Studio Intellisense
Figure 1: Visual Studio Intellisense

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read