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



Related Articles

Comments

  • Option Strict

    Posted by jwooley on 05/31/2011 11:06am

    Realize that your example of initializing a double would not work with Option Strict turned On (as you should do by default to avoid these kinds of type coersion issues). Actually, to initialize a double, simply use the following: Dim num As Double = 3.1415 If you use type inference, this can be simplified to Dim num = 3.1415 If you want to make this a decimal instead, use the decimial literal: Dim num = 3.1415D For a full listing of the VB literals see http://msdn.microsoft.com/en-us/library/dzy06xhf.aspx#Y608.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds