Reading — step 1 of 5
Learn
VB.NET uses Namespace for organization and Module for static-only containers.
Namespaces
Namespace MyCompany.Utils
Public Class Logger
Public Sub Log(msg As String)
Console.WriteLine($"[LOG] {msg}")
End Sub
End Class
End Namespace
' Use:
Dim l = New MyCompany.Utils.Logger()
Imports
Imports MyCompany.Utils
' Now Logger is directly accessible:
Dim l = New Logger()
Imports aliases:
Imports U = MyCompany.Utils
Dim l = New U.Logger()
Modules — VB's static-only equivalent
A Module is like a C# static class. Everything inside is implicitly Shared:
Module MathUtils
Public Function Square(n As Integer) As Integer
Return n * n
End Function
Public Const Pi As Double = 3.14159
End Module
' Call directly — no instance needed:
MathUtils.Square(5)
MathUtils.Pi
For a single-file program, the Module Program containing Sub Main() is conventional. For libraries, prefer Class over Module (more flexible).
Standard library namespaces
Most-used .NET namespaces:
System— primitives,Console,ObjectSystem.Collections.Generic—List(Of T),Dictionary, etc.System.Linq— LINQ extension methodsSystem.IO— file I/OSystem.Net— networkingSystem.Text— encoding, StringBuilderSystem.Text.RegularExpressions— regexSystem.Threading.Tasks— async/TaskSystem.Xml,System.Json— serialization
Imports patterns
Implicit imports — Imports lines auto-added by Visual Studio:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Usually present at the top of every file.
Friend (assembly-internal)
Friend Class Helper
Public Sub DoStuff()
End Sub
End Class
Friend is like C#'s internal — visible across files in the same assembly but not outside. Useful for library implementation details.
Project organization
A typical VB.NET project (.vbproj):
MyApp/
MyApp.vbproj
Program.vb ' contains Module Program with Sub Main
Models/
User.vb
Order.vb
Services/
UserService.vb
OrderService.vb
Each file declares its Namespace (matching folder structure is convention, not required).
Why Modules persist
For utility code (parsing, formatting, math), Module saves typing — no Class boilerplate, no Shared keyword on every method. Many VB6-style helpers continue this pattern.
For object-oriented design, use Class. Modules are for stateless function collections.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…