Dealing with Data
In our daily life, we work with many kinds of information such as names, money, phone numbers, addresses, dates, prices, stock quotes, marks, and more. In Visual Basic 2015, we also need to handle many kinds of data. Some data can be calculated mathematically, while others represent text, dates, or logical values. Understanding data types is essential because it helps you store values correctly and perform the right kinds of operations on them.
Before you can write useful programs, you must understand the types of data your program will handle. In VB2015, data can be stored as variables, constants, or arrays, and each item of data belongs to a specific data type.
Lesson Overview
8.1 Introduction
Understanding Data in Visual Basic 2015
In Visual Basic 2015, data can be stored as variables, constants, or arrays. A variable is like a storage box whose content can change. A constant is like a fixed label whose value stays the same throughout the program. An array stores many related values under one name.
Data in Visual Basic 2015 can be grouped into two major categories:
- Numeric data types — values that can be calculated mathematically.
- Non-numeric data types — values such as text, dates, and logical values.
8.1.1 Numeric Data Types
Numeric Data That Can Be Calculated
Numeric data types are used for values such as examination marks, body weight, product prices, bus fares, monthly bills, fees, and other numbers that may be involved in calculations.
Visual Basic 2015 provides several numeric data types. Each one has its own storage size and range of values. Small values can use smaller data types, while larger or more precise values require more storage.
| 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 | Floating-point values with moderate precision |
| Double | 8 bytes | Floating-point values with higher precision |
| Currency | 8 bytes | -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
| Decimal | 12 bytes | Very high precision decimal values |
When you only need whole numbers, you may use Integer or Long. When you need decimal places, you often use Single or Double. For financial calculations, Currency is often more appropriate.
8.1.2 Non-Numeric Data Types
Data That Cannot Be Calculated Mathematically
Not all data is meant for arithmetic. Many values in real programs are text, dates, logical values, or objects. These belong to the non-numeric data types.
| Type | Storage | Range / Description |
|---|---|---|
| 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 |
String is used for text such as names and addresses. Date stores dates and times. Boolean stores only two values: True or False.
8.1.3 Literal Suffixes
Suffixes for Literals
A literal is a value written directly in your code. Sometimes, you add a suffix after a literal so that Visual Basic 2015 understands the type more clearly.
For example:
num = 1.3089!
num = 1.3089#
num = 130890&
num = 1.3089@
The suffixes above tell Visual Basic to treat the values as Single, Double, Long, and Currency respectively.
| Suffix | Data Type |
|---|---|
| & | Long |
| ! | Single |
| # | Double |
| @ | Currency |
8.2 Examples
String and Date Literals
In Visual Basic 2015, string literals are enclosed in quotation marks, while date and time literals are enclosed within two # symbols.
memberName = "Turban, John."
TelNumber = "1800-900-888-777"
LastDay = #31-Dec-00#
ExpTime = #12:00 am#
Notice that a telephone number contains digits, but it is still treated as a string because it is not intended for mathematical calculation.
8.3 Why This Matters
Choosing the Right Data Type
Choosing the correct data type helps your program store values efficiently and correctly. It also helps prevent errors in calculations, comparisons, and formatting. As your programs become more complex, understanding data types becomes even more important.
Recommended Upgrade
Build on This Foundation
Continue to VB2026
After learning the basics of data types in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.
Visual Basic Programming
Use this Top Release book to reinforce your tutorial learning with a more structured guide.
Practice
Exercise Questions
- What is the difference between numeric and non-numeric data types in Visual Basic 2015?
- When would you choose Double instead of Integer?
- Write one example each of a string literal and a date literal in VB2015.