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

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date