VB.NET Data Types and Option Strict
VB.NET provides a full set of built-in value types — Integer (32-bit), Long (64-bit), Double and Decimal for numbers, Boolean, Char, and the reference type String — and every type maps directly to a .NET Framework type, so an Integer is really System.Int32 and a String is System.String, meaning VB.NET's type system is identical in capability to C#'s, just spelled differently.
Cricket analogy: It's like how 'a century' always means exactly 100 runs whether the commentator is speaking English or Hindi, the underlying quantity is fixed — similarly VB.NET's Integer is always .NET's System.Int32 under the hood, just labeled differently.
Value Types vs Reference Types
Value types like Integer, Double, and Boolean are stored directly on the stack (or inline within an object) and copied by value when assigned, so Dim b As Integer = a creates an independent copy, whereas reference types like String, arrays, and custom classes are stored on the managed heap and assigned by reference, so two variables can point at the same underlying object and a change through one reference is visible through the other, except that String is immutable so operations like concatenation always produce a new object rather than mutating the original.
Cricket analogy: It's like handing a teammate a photocopy of your batting stats sheet — they can scribble on their copy without changing yours, just as Dim b As Integer = a copies the value; but handing them the actual scorebook itself means their edits show up on your original too, like two variables referencing the same array.
Option Strict and Option Explicit
Option Strict On (which is off by default in classic VB.NET project templates but strongly recommended) disallows implicit narrowing conversions, such as assigning a Double directly to an Integer variable or an Object to a specific type, forcing you to use an explicit conversion function instead; Option Explicit On, which is on by default, requires every variable to be declared with Dim before use, preventing typos like Dim totl As Integer from silently creating a brand-new variable instead of catching the misspelling.
Cricket analogy: It's like a strict umpire who won't allow a batsman to walk onto the field without the correct kit checked first — Option Explicit forces every variable to be formally 'checked in' with Dim before use, catching a mistyped name like totl instead of total before it silently creates a new, wrong player.
Type Conversion Functions
When Option Strict is on, you convert between types explicitly using functions like CInt(), CDbl(), CStr(), and CBool(), or the more robust Convert.ToInt32() and TryParse() methods that handle bad input gracefully instead of throwing an exception; CInt() specifically rounds to the nearest integer (so CInt(2.5) yields 2 due to banker's rounding) which differs from CType() and DirectCast(), which perform type conversions without that rounding behavior for numeric narrowing.
Cricket analogy: It's like a scorer choosing between rounding a bowling average up or down using a specific ICC-approved rounding rule rather than guessing, similar to CInt() applying banker's rounding (CInt(2.5) = 2) while CType() performs a plain narrowing conversion without that special rounding rule.
Option Strict On
Dim price As Double = 19.99
Dim quantity As Integer = CInt(3)
Dim total As Double = price * quantity
Dim totalText As String = total.ToString("C")
Console.WriteLine(totalText)With Option Strict Off, code like Dim x As Integer = "42" compiles silently and only fails at runtime if the string can't be parsed — always turn Option Strict On (Project Properties > Compile, or a project-wide setting) to catch these type errors at compile time instead of in production.
- Every VB.NET built-in type maps directly to a .NET Framework type, e.g. Integer is System.Int32.
- Value types like Integer and Double are copied by value; reference types like String and arrays are shared by reference.
- String is immutable, so operations like concatenation always produce a new String object.
- Option Strict On disallows implicit narrowing conversions, catching type errors at compile time.
- Option Explicit On requires every variable to be declared with Dim before use, catching typos.
- CInt() applies banker's rounding, differing from CType() and DirectCast(), which don't round the same way.
- TryParse() and Convert.ToInt32() handle type conversion more gracefully than an unguarded implicit cast.
Practice what you learned
1. What .NET Framework type does VB.NET's Integer map to?
2. What happens when you assign one value-type variable to another, e.g. Dim b As Integer = a?
3. What does Option Strict On prevent?
4. What does Option Explicit On require?
5. What is the result of CInt(2.5) in VB.NET?
Was this page helpful?
You May Also Like
VB.NET Syntax and Variables
Core VB.NET syntax rules — statements, case-insensitivity, variable declarations with Dim, naming conventions, scope, and comments.
What Is VB.NET?
An introduction to VB.NET, a modern, object-oriented .NET language derived from classic Visual Basic, covering its history, ecosystem role, and comparison to C#.
Your First VB.NET Program
Writing, compiling, and running a minimal VB.NET console program, and understanding Sub Main, Console.WriteLine, and the build/run workflow.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics