Modules, Namespaces and Project Organization
16.1 Modules
A module is like a C# static class. It can't be instantiated or inherited. Its members are automatically in scope within the namespace.
Public Module MathUtils ' all members are implicitly Shared
Public Const Golden As Double = 1.618
Public Function Square(x As Double) As Double
Return x * x
End Function
End Module
Console.WriteLine(Square(4)) ' module members are accessible without a prefix
Console.WriteLine(MathUtils.Square(4)) ' or qualified16.2 Namespaces
The project's Root Namespace (<RootNamespace>) is prepended to all namespaces declared in the project.
Project-level imports (<Import Include="System.Linq" /> in .vbproj, or Project Properties → References → Imported namespaces) apply to every file.
Namespace Company.Sales
Public Class Order
End Class
Namespace Reports
Public Class SalesReport
End Class
End Namespace
End Namespace
Imports Company.Sales ' at the top of the file
Imports Rpt = Company.Sales.Reports ' alias
Imports System.Math ' import a type's shared members: Sqrt(16)
Imports <xmlns:ns="http://example.com/ns"> ' XML namespace import (Module 28)
Dim o As New Order()
Dim r As New Rpt.SalesReport()
Dim x = Global.System.Math.PI ' Global: start from the root namespace16.3 Access Modifiers
The same assembly (project) The class and derived classes Protected Friend
The same assembly or derived classes Private Protected (VB 15.5)
Derived classes within the same assembly The containing type/module only
16.4 Partial Classes and Files
Used heavily by designers (Form1.vb + Form1.Designer.vb). Partial methods (Partial Private Sub OnCreated()) are optional hooks.
' Customer.vb
Partial Public Class Customer
Public Property Name As String
End Class
' Customer.Validation.vb
Partial Public Class Customer
Public Function IsValid() As Boolean
Return Not String.IsNullOrWhiteSpace(Name)
End Function
End Class16.5 Solution Structure Example
Add project references: dotnet add MyApp.UI reference MyApp.Core.
MySolution.sln
├── MyApp.UI/ (WinForms / WPF project)
├── MyApp.Core/ (Class library: business logic)
├── MyApp.Data/ (Class library: data access)
└── MyApp.Tests/ (Test project)