VB.NET
casting
programming
software development
data types

How should I cast in VB.NET?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In VB.NET, the right casting approach depends on what kind of conversion you need and whether failure is expected. The practical rule is to keep Option Strict On, then choose between DirectCast, TryCast, CType, and the numeric conversion functions based on intent.

Start with Option Strict On

This setting is the foundation for sane casting rules:

vb
Option Strict On

With Option Strict On, VB.NET stops allowing many implicit narrowing conversions. That is good because it forces potentially unsafe casts to be explicit and visible in code review.

Use DirectCast for Known Reference-Type Casts

DirectCast is for reference conversions where the runtime type is expected to be compatible already.

vb
Dim obj As Object = New Button()
Dim btn As Button = DirectCast(obj, Button)
btn.Text = "Save"

This is appropriate when you are confident about the type. If the runtime object is not actually a Button, the cast throws an exception.

Use DirectCast when:

  • the conversion is between related reference types
  • you know the cast should succeed
  • you want a straightforward runtime type check

Use TryCast When Failure Is Normal

TryCast is safer for uncertain reference casts because it returns Nothing instead of throwing.

vb
1Dim obj As Object = GetControl()
2Dim btn As Button = TryCast(obj, Button)
3
4If btn IsNot Nothing Then
5    btn.Text = "OK"
6End If

This is ideal in UI code, reflection-heavy code, or polymorphic code where a failed cast is part of ordinary control flow.

The key limitation is that TryCast works only with reference types and nullable types. It does not handle arbitrary numeric conversions.

Use CType for General Conversions

CType is broader than DirectCast. It can use conversion operators and built-in conversion rules.

vb
Dim s As String = "42"
Dim n As Integer = CType(s, Integer)
Dim d As Double = CType(n, Double)

This is the right tool when you are converting values rather than just reinterpreting a reference type. It is also the cast that user-defined conversion operators can plug into.

For business code, CType often expresses intent better than forcing everything through intermediate Object variables.

Use Conversion Functions for Primitive Targets

For common primitives, the dedicated conversion functions are often the clearest choice:

vb
1Dim s As String = "42"
2Dim i As Integer = CInt(s)
3Dim amount As Decimal = CDec("19.95")
4Dim text As String = CStr(123)

These are easy to read and very common in VB.NET codebases. They are usually the best option when the target type is a built-in numeric or text type.

A Practical Decision Rule

Use this mental checklist:

  • 'TryCast for uncertain reference casts'
  • 'DirectCast for guaranteed compatible reference casts'
  • 'CType for broader conversions and user-defined conversion logic'
  • 'CInt, CDbl, CStr, and similar functions for primitive conversions'

That rule covers most real code without needing to memorize edge cases.

Validate External Input

Casting is not validation. If a string comes from a user or a file, convert defensively.

vb
1Dim raw As String = "123"
2Dim value As Integer
3
4If Integer.TryParse(raw, value) Then
5    Console.WriteLine(value)
6Else
7    Console.WriteLine("Invalid number")
8End If

For user input, TryParse is usually better than a direct cast because failure is expected and should be handled cleanly.

Common Pitfalls

  • Leaving Option Strict Off, which allows risky implicit conversions and hides bugs.
  • Using DirectCast when a failed cast is normal and should not throw.
  • Using TryCast for numeric conversions, which it does not support.
  • Treating CType as the answer to every conversion question when a specific conversion function is clearer.
  • Casting external input without validation instead of using TryParse.

Summary

  • Keep Option Strict On so narrowing conversions stay explicit.
  • Use DirectCast for known compatible reference casts.
  • Use TryCast when a reference cast may legitimately fail.
  • Use CType or conversion functions such as CInt for value conversions.
  • For user input, prefer TryParse over blind casting.

Course illustration
Course illustration

All Rights Reserved.