VB.NET
C#
Programming
Type Conversion
Code Syntax

VB.NET equivalent of C As

Master System Design with Codemia

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

Introduction

In C#, the as operator performs safe reference casting and returns null when conversion fails. In VB.NET, the closest equivalent is TryCast, which returns Nothing instead of throwing an exception. Understanding when to use TryCast, DirectCast, and CType helps you write predictable conversion logic across both languages.

Map C# as to VB.NET TryCast

C# style:

csharp
1object value = GetUnknownObject();
2var stream = value as System.IO.Stream;
3if (stream != null)
4{
5    Console.WriteLine(stream.Length);
6}

VB.NET equivalent using TryCast:

vbnet
1Dim value As Object = GetUnknownObject()
2Dim stream As System.IO.Stream = TryCast(value, System.IO.Stream)
3
4If stream IsNot Nothing Then
5    Console.WriteLine(stream.Length)
6End If

TryCast only works for reference types and returns Nothing if conversion is invalid.

Know the Difference Between TryCast, DirectCast, and CType

These three operators are often confused:

  • TryCast: safe for reference conversions, returns Nothing on failure.
  • DirectCast: requires compatible runtime type, throws on invalid cast.
  • CType: can perform broader conversions through conversion operators.

Example:

vbnet
1Dim obj As Object = "42"
2
3' Safe cast for reference type
4Dim text As String = TryCast(obj, String)
5
6' Runtime-checked cast, throws if wrong type
7Dim mustBeString As String = DirectCast(obj, String)
8
9' Conversion, not just cast
10Dim number As Integer = CType(obj, Integer)

Use TryCast when type compatibility is uncertain and you want branch-based control flow.

Practical Pattern With Interfaces

TryCast is especially useful with interface checks in event-driven or plugin-heavy code.

vbnet
1Public Interface ILoggable
2    Sub Log()
3End Interface
4
5Public Class Worker
6    Implements ILoggable
7
8    Public Sub Log() Implements ILoggable.Log
9        Console.WriteLine("Worker log entry")
10    End Sub
11End Class
12
13Module Program
14    Sub Main()
15        Dim anyObj As Object = New Worker()
16        Dim loggable As ILoggable = TryCast(anyObj, ILoggable)
17
18        If loggable IsNot Nothing Then
19            loggable.Log()
20        Else
21            Console.WriteLine("Object does not support logging")
22        End If
23    End Sub
24End Module

This avoids exceptions during normal runtime feature detection.

Nullable and Value Type Considerations

TryCast does not handle value type conversion. For numeric conversion and nullable value types, use Integer.TryParse, CType, or explicit conversion APIs.

vbnet
1Dim input As String = "123"
2Dim parsed As Integer
3
4If Integer.TryParse(input, parsed) Then
5    Console.WriteLine(parsed)
6Else
7    Console.WriteLine("Invalid integer")
8End If

For Object to value type conversion, a failed direct cast throws. Prefer parse or checked conversion when input is external.

Translation Guidance for Mixed C# and VB.NET Teams

When porting C# to VB.NET, conversion intent matters more than keyword mapping.

A quick translation rule:

  • C# as with null check usually becomes VB.NET TryCast with IsNot Nothing.
  • C# explicit cast with guaranteed type usually maps to DirectCast.
  • C# conversion operations may need CType or TryParse in VB.NET.

Keep unit tests around conversion logic during translation, because runtime behavior differences can surface only in edge cases.

Performance and Readability Notes

Micro-optimizing cast operators is rarely useful compared with making conversion intent explicit. Most bugs here come from unclear assumptions about input type, not cast speed.

Prefer code that communicates safety clearly:

vbnet
1Dim customer As Customer = TryCast(candidate, Customer)
2If customer Is Nothing Then
3    Return
4End If
5
6ProcessCustomer(customer)

This is easier to maintain than broad Try...Catch blocks around invalid casts.

Common Pitfalls

  • Assuming TryCast works with value types such as Integer.
  • Replacing every cast with CType and losing safe-failure semantics.
  • Using DirectCast when source type is uncertain.
  • Forgetting Nothing checks after TryCast.
  • Translating C# code mechanically without matching conversion intent.

Summary

  • VB.NET equivalent of C# as is TryCast for reference types.
  • TryCast returns Nothing instead of throwing on mismatch.
  • Use DirectCast only when type compatibility is guaranteed.
  • Use parsing or conversion APIs for value type scenarios.
  • Prioritize clarity of conversion intent during C# and VB.NET translation.

Course illustration
Course illustration

All Rights Reserved.