DEV Community

DIVAKAR UPADHYAY
DIVAKAR UPADHYAY

Posted on Originally published at devladder.pages.dev

Data types, variables, constants and conversions (VB.NET)

👉 Full lesson with practice questions on DevLadder (free, lesson 2 of 12 in the VB.NET: Visual Basic .NET from Basics to Advanced (with C# Comparison) course)

VB.NET C# .NET type Literal
Integer int Int32 42, 42I
Long long Int64 42L
Short / Byte short / byte Int16 / Byte 42S
Double / Single double / float Double / Single 1.5R / 1.5F
Decimal decimal Decimal 1.5D
Boolean bool Boolean True / False
Char char Char "A"c
String string String "text"
Date DateTime DateTime #10/24/2026# (M/d/yyyy)
Object object Object
Dim count As Integer = 10
Dim price = 99.5D                 ' Decimal, inferred with Option Infer On
Const TaxRate As Decimal = 0.18D
Dim total As Decimal = price * (1 + TaxRate)
Dim nothingYet As String = Nothing     ' Nothing = C# null (or default for value types)
Dim maybe As Integer? = Nothing        ' Nullable(Of Integer)

' Conversions
Dim n1 As Integer = CInt("42")          ' VB conversion keyword (rounds to even for decimals: CInt(2.5) = 2)
Dim n2 As Integer = Integer.Parse("42")
Dim ok As Boolean = Integer.TryParse(input, n1)
Dim d As Double = CDbl(n1)
Dim s As String = n1.ToString()
Dim o As Object = "hello"
Dim str As String = DirectCast(o, String)   ' cast, throws if wrong type (C# (string)o)
Dim str2 As String = TryCast(o, String)     ' returns Nothing if wrong type (C# o as string)
Dim v = CType(o, String)                    ' general conversion
Enter fullscreen mode Exit fullscreen mode

Option Strict On: the most important setting: it forbids implicit narrowing conversions (Dim i As Integer = 3.7 fails) and late binding on Object. With it Off, VB silently converts types and resolves members at runtime: a source of hidden bugs. Turn it on for every project (Project Properties → Compile).

Interview questions on VB.NET

What is the difference between VB.NET and C#?

Both compile to the same IL and run on the same CLR with the same libraries, so performance is the same. Differences are syntax and features: VB.NET uses English-like keywords, is case-insensitive, line-based with End blocks, has XML literals, My namespace, WithEvents/Handles and optional late binding; C# uses C-style syntax, is case-sensitive and gets new language features every year while VB.NET has been feature-stable since 2020. Most new projects use C#; VB.NET is mainly maintained in legacy systems.

What do Option Strict, Option Explicit and Option Infer do?

Option Explicit On requires variables to be declared. Option Strict On disallows implicit narrowing conversions and late binding: catching type errors at compile time (strongly recommended). Option Infer On lets Dim x = 5 infer the type from the initializer. Set them per project (Compile settings) or per file at the top.

What is the difference between Sub and Function?

A Sub performs an action and returns no value (C# void method). A Function returns a value with Return (or by assigning to the function name in old code). Both can take ByVal/ByRef, Optional and ParamArray parameters.


Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.

Next in the course: Operators, conditions and loops →

Top comments (0)