Visual Basic
Variables and Types
Declare variables with Dim and type inference.
By EZ4Code Team
variablestypes
Code
' Explicit type
Dim age As Integer = 30
Dim name As String = "Alice"
Dim height As Double = 1.75
Dim isActive As Boolean = True
' Type inference (Option Infer On)
Dim count = 10 ' Integer
Dim message = "Hello" ' String
' Constants
Const PI As Double = 3.14159
' Nullable
Dim score As Integer? = Nothing
If score.HasValue Then
Console.WriteLine(score.Value)
End If
' Object (late binding, Option Strict Off)
Dim obj As Object = "text"
Dim len As Integer = obj.Length ' late-boundExplanation
VB uses Dim for declarations. Option Infer On enables type inference (like var in C#). Option Strict On enforces type safety (recommended). Nullable types (Of T?) wrap value types to support Nothing. Use Object only for late binding with COM/interop.
More Visual Basic Snippets
Loops and Iteration
For, For Each, While, and Do loops in VB.
Sub and Function Procedures
Define Sub (no return) and Function (returns value).
Windows Forms Basics
Create a simple WinForms application.
File I/O
Read/write text files and use My.Computer.FileSystem.
Error Handling (Try/Catch)
Structured exception handling in VB.
Collections (List, Dictionary)
Generic collections in VB.NET.