Visual Basic

What Are The Visual Basic Data Types

Visual Basic data types specify what a variable can store and which operations it supports. In VB.NET, they include whole-number types such as Integer and Long, numeric types such as Single, Double and Decimal, plus String, Char, Boolean, Date and Object. Choosing a type means.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

Which Visual Basic does this guide cover?

This guide covers Visual Basic for .NET, often called VB.NET. Older Visual Basic and Visual Basic for Applications (VBA) use some familiar names differently. For example, Integer and Long have different sizes in VBA, and Variant belongs to older Visual Basic and VBA rather than the VB.NET type system. Check the language used by your project before copying declarations.

If you are setting up your development environment, the guide to where to buy Visual Basic provides related context. The data types below concern the language used in your code.

Choose a data type by the value it represents

A variable's type affects more than storage. It determines whether a value can contain a fraction, whether text operations are available and whether a conversion might lose information. These common types provide a starting point; Microsoft's Visual Basic data type reference lists their full ranges.

Type Stores Typical use
Byte Whole numbers from 0 to 255 Binary data
Short Signed 16-bit whole numbers Values required by a particular data format
Integer Signed 32-bit whole numbers Counts and loop counters
Long Signed 64-bit whole numbers Large counts and numeric identifiers
Single, Double Binary floating-point numbers Measurements and scientific calculations
Decimal Decimal numbers with bounded precision Prices and financial calculations
Char, String A UTF-16 character unit or a sequence of text Character processing, names and messages
Boolean True or False Conditions and status flags
Date A date and time Appointments and recorded events
Object A reference that can hold any .NET type APIs that require a general object value

Whole numbers: Integer, Long and smaller types

Integer is a practical starting point for ordinary whole-number calculations. Its range is -2,147,483,648 to 2,147,483,647. Use Long when legitimate values can exceed that range. Short holds -32,768 to 32,767; Byte holds only non-negative values up to 255.

VB.NET also provides SByte, UShort, UInteger and ULong. The unsigned types cannot represent negative values, so choose them only when that restriction fits the data or an interface requires them.

Allow for calculation results

Check intermediate results as well as stored inputs. Multiplying two valid values can produce a result outside the expression's type range. Assigning that result to a larger variable does not automatically make the earlier arithmetic safe.

A value below an integer type's minimum is an overflow condition, just as a value above its maximum is. Do not assume that choosing the smallest possible type makes code faster; range and compatibility are better starting points.

Fractional values: Single, Double and Decimal

Single and Double use binary floating-point representation. Double provides more precision than Single and is a common choice for measurements and mathematical functions. Neither can represent every decimal fraction exactly, so calculations can introduce small rounding differences.

Decimal uses decimal scaling and is usually a better fit for money. It can represent values such as 0.1 exactly, but its precision and range are still limited. Division and other calculations can require rounding, so a financial application still needs an explicit rounding policy. Microsoft's numeric data types guide explains the distinction between Decimal and floating-point types.

Declare decimal literals deliberately: Dim price As Decimal = 19.95D uses the Decimal literal suffix. For a mathematical example involving Double, see how to write a square root in Visual Basic.

Text: String and Char

String stores text, including values that happen to contain digits. A postcode, telephone number or reference with leading zeros often belongs in a String because arithmetic would not make sense for it. Char stores one UTF-16 character unit; some displayed symbols require more than one such unit.

A declaration such as Dim customerName As String = "Alex" makes the intent explicit. Use & to join strings. Methods such as Replace and Substring return text results; strings themselves are immutable. Microsoft's string basics guide explains string literals and their behaviour.

Write quotation marks and line breaks correctly

Inside a string literal, double a quotation mark to include it: "She said ""Hello""". Ordinary VB string literals do not interpret \n or \t as escape sequences. Join text with vbCrLf for a carriage return and line feed, or vbTab for a tab, using the language's character constants. A backslash can appear directly in a string literal.

Boolean, Date and Object

Boolean represents True or False. For example, Dim isReady As Boolean = True declares a status flag. Comparisons also produce Boolean results, which you can use in If statements and loops. Use meaningful names so that conditions read clearly.

Date represents a date and time through .NET's DateTime type. Store dates as date values when you need to compare them or perform date arithmetic, then format them as text for display.

Object can hold any .NET type, but it gives the compiler less specific information about the value. Prefer a concrete type when you know what a variable represents. Arrays group elements of a declared type, while structures and classes let you define types that combine related data and behaviour.

Declare and convert values deliberately

  1. Identify the meaning of the value: text, a count, a measurement, money, a condition or a date.
  2. Check the largest and smallest expected values, including calculation results, and decide how much precision is needed.
  3. Declare the type with an As clause, such as Dim quantity As Integer = 10. Local type inference is also available when Option Infer is On.
  4. Enable Option Strict On to catch implicit narrowing conversions and late binding. Microsoft documents these checks in the Option Strict reference.
  5. Validate input before conversion. An explicit conversion expresses intent but can still fail or lose information.

Choose one calculation in your project and inspect its inputs, intermediate results and destination type. Correct any range or precision mismatch, then try boundary values before moving to another example from the Visual Basic guides.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post