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.
| 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.
| 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.
| 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:
' 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:
8.2.1 Adding Numbers
Adding two numbers is a valid operation. VB2019 will perform the arithmetic addition.
Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles MyBase.Load ' Adding two numbers MsgBox(10 + 20.5) End Sub
Output:
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.
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:
8.2.3 Concatenating Strings
Concatenating two strings is a valid operation. You can use either the + operator or the & operator for string concatenation.
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:
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
Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.
Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.