Lessons in this course (12)

2. Data types, variables, constants and conversions

Basic★ Must knowLesson 2 of 12
VB.NETC#.NET typeLiteral
IntegerintInt3242, 42I
LonglongInt6442L
Short / Byteshort / byteInt16 / Byte42S
Double / Singledouble / floatDouble / Single1.5R / 1.5F
DecimaldecimalDecimal1.5D
BooleanboolBooleanTrue / False
CharcharChar"A"c
StringstringString"text"
DateDateTimeDateTime#10/24/2026# (M/d/yyyy)
ObjectobjectObject
vb
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

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).