Lesson 8: Data Types in VB2019

Master data representation and manipulation for robust application development

Key Takeaway

Understanding data types is fundamental to programming in Visual Basic. Choosing the right data type ensures efficient memory usage, prevents errors, and enables accurate calculations.

In our daily life, we have to deal with all kinds of information and data. For example, we need to deal with data like names, money, phone numbers, addresses, dates, and more. Similarly, in Visual Basic 2019, we have to deal with all sorts of data, some of them can be mathematically calculated while some are in the form of text or other non-numeric forms.

8.1 Understanding Data Types

Visual Basic 2019 classifies information into two major data types: numeric data types and non-numeric data types. Choosing the appropriate data type is crucial for efficient memory usage and accurate calculations.

Numeric Data Types

Numeric data types represent values that can be used in mathematical operations. They are ideal for calculations, measurements, and financial operations.

Non-Numeric Data Types

Non-numeric data types represent textual, logical, or temporal information. They are used for storing names, descriptions, dates, and true/false values.

8.1.1 Numeric Data Types

In Visual Basic 2019, numeric data are classified into seven types based on the range of values they can store. Numeric data that involve round figures are classified as Integer or Long integer. Data that require high precision calculation are classified as single and double precision data types (also called floating point numbers). Numeric data that involve money are classified as currency data types. Lastly, data that require more precision and involve many decimal points are classified as decimal data types.

Table 8.1: Numeric Data Types
Type Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
Single 4 bytes -3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values
Double 8 bytes -1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is used
+/- 7.9228162514264337593543950335 (28 decimal places)

Memory Considerations

Choosing the appropriate data type helps optimize memory usage. For example, use Byte for small positive integers (0-255), Integer for whole numbers within ±32k, and Long for larger integers.

8.1.2 Non-numeric Data Types

In Visual Basic 2019, non-numeric data types are data that cannot be calculated using standard arithmetic operators. The non-numeric data comprises text or string data types, the Date data types, the Boolean data types that store only two values (true or false), Object data type and Variant data type.

Table 8.2: Non-numeric Data Types
Type Storage Range
String (fixed length) Length of string 1 to 65,400 characters
String (variable length) Length + 10 bytes 0 to 2 billion characters
Date 8 bytes January 1, 100 to December 31, 9999
Boolean 2 bytes True or False
Object 4 bytes Any embedded object
Variant (numeric) 16 bytes Any value as large as Double
Variant (text) Length+22 bytes Same as variable-length string

Important Notes

String Handling: Strings are immutable in VB2019 - each modification creates a new string object. For intensive string operations, consider using StringBuilder.

Variant Type: Avoid using Variant unless necessary as it consumes more memory and is slower than specific types.

8.1.3 Suffixes for Literals

In Visual Basic 2019, we can add a suffix behind a literal so that Visual Basic can handle the calculations more accurately. For example, we can use num=1.3089! for a single precision data type, num=1.3089# for a double precision data type, num=130890& to indicate long integer data type and num=1.3089@ to indicate currency data type.

Table 8.3: Literal Suffixes
Suffix Data Type Example
& Long 150000&
! Single 3.14159!
# Double 3.1415926535#
@ Currency 19.99@

In addition, we need to enclose string literals within two quotations and date and time literals within two # signs. Strings can contain any characters, including numbers. Examples:

LiteralExamples.vb
' String literals
Dim memberName As String = "Turban, John."
Dim telNumber As String = "1800-900-888-777"

' Date literals
Dim lastDay As Date = #31-Dec-2024#
Dim expTime As Date = #12:00 am#

' Numeric literals with suffixes
Dim population As Long = 7800000000&
Dim pi As Double = 3.1415926535#
Dim price As Currency = 19.99@

8.2 Performing Operations on Data

When VB2019 performs operations using arithmetic operators, it needs to understand the data types. It can perform mathematical operations on data of numeric nature but not a mix of numeric data and non-numeric data.

1 Adding Numbers

Valid operation: Numeric + Numeric

2 Adding Number and String

Invalid operation: Causes runtime error

3 Concatenating Strings

Valid operation: String + String or String & String

Data Operations Demo

Try different operations to see how VB2019 handles various data type combinations.

Output:

Select an operation to see the result

8.2.1 Adding Numbers

Adding two numbers is a valid operation. VB2019 will perform the arithmetic addition.

Form1.vb
Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Adding two numbers
    MsgBox(10 + 20.5)
End Sub

Output:

> Result: 30.5
Adding numbers result
Figure 8.1: Adding numbers produces a numeric result

8.2.2 Adding a Number and a String

Adding a number to a string is invalid and causes a runtime error. VB2019 cannot perform arithmetic operations on mixed data types.

Form1.vb
Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Attempt to add a number and a string
    MsgBox(10 + "John")
End Sub

Output:

> Error: Conversion from string "John" to type 'Double' is not valid.
Error when adding number and string
Figure 8.2: Error when adding number and string

8.2.3 Concatenating Strings

Concatenating two strings is a valid operation. You can use either the + operator or the & operator for string concatenation.

Form1.vb
Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Concatenate two strings
    MsgBox("George" + " John")
    
    ' Alternative using & operator
    MsgBox("George" & " John")
End Sub

Output:

> Result: George John
Concatenating strings result
Figure 8.3: Concatenating strings produces a combined string

Best Practice

When working with strings, prefer the & operator over the + operator for concatenation. This makes your intention clear and prevents accidental arithmetic operations when one operand is numeric.

Lesson Summary

In this lesson, you've learned about data types in Visual Basic 2019 and how to perform operations with them:

Numeric Data Types

Understand the range and purpose of Byte, Integer, Long, Single, Double, Currency, and Decimal

Non-Numeric Data Types

Work with Strings, Dates, Booleans, Objects, and Variants

Literal Suffixes

Use &, !, #, and @ to specify data types for literals

Operations

Perform arithmetic operations on numbers and concatenation on strings

You've learned how to work with different data types in Visual Basic 2019. Understanding data types is essential for writing robust and efficient applications. In the next lesson, we'll explore how to declare and use variables and constants.

Next Lesson

Ready to learn about variables and constants? Continue to Lesson 9: Variables and Constants.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon