The types of a program are declared in namespaces. Visual Basic has five kinds of type declaration besides enumerations (see types and variables): classes, structures, standard modules, interfaces and delegates. A class has single inheritance (Inherits) and can implement any number of interfaces (Implements); every class derives from System.Object.
Classes
Public Interface IAnimal
ReadOnly Property Name As String
Sub Speak()
End Interface
Public MustInherit Class Animal
Implements IAnimal
Public ReadOnly Property Name As String Implements IAnimal.Name
Protected Sub New(name As String)
Me.Name = name
End Sub
Public MustOverride Sub Speak() Implements IAnimal.Speak
Public Overloads Overridable Function Describe() As String
Return "An animal called " & Name
End Function
Public Overloads Function Describe(prefix As String) As String
Return prefix & Describe()
End Function
End Class
Public Class Dog
Inherits Animal
Public Sub New(name As String)
MyBase.New(name)
End Sub
Public Overrides Sub Speak()
Console.WriteLine("Woof")
End Sub
Public Overrides Function Describe() As String
Return "A dog. " & MyBase.Describe()
End Function
End Class
| Modifier | Meaning |
|---|---|
MustInherit, NotInheritable | an abstract class (cannot be created, only inherited) and a sealed class (cannot be inherited) |
MustOverride, Overridable, NotOverridable | a member with no body that derived classes must implement; a member that may be overridden; a member that overrides and cannot be overridden again |
Overrides | replaces an inherited Overridable, MustOverride or Overrides member with the same signature |
Overloads | adds a member of the same name with a different signature; required on all of them once one of the overloads in the hierarchy uses Overloads or Overrides |
Shadows | hides an inherited member of that name whatever its signature (it is not an override) |
Shared | a member that belongs to the type, not to an object (a Shared Sub New is a type initialiser that runs once) |
Partial | the class continues in another declaration of the same name (version 8; interfaces and modules too, version 14) |
ReadOnly, WriteOnly, WithEvents, Default, Widening, Narrowing | on properties, fields and operators as described on the procedures page |
Accessibility. Public (everywhere), Friend (the same assembly, or assemblies given InternalsVisibleTo), Protected (the class and its derived classes), Protected Friend (either), Private (the declaring type and what is nested in it) and, since version 15.5, Private Protected (derived classes in the same assembly). Members of a class, structure or module are Public unless they are variables, which are Private (in a structure, variables are Public); top-level types are Friend by default.
Constructors. Sub New is the constructor. If a class declares none, a default one is provided (Protected for a MustInherit class). A constructor starts with a call to another constructor of the same class (Me.New(…)) or the base class (MyBase.New(…)), and if it leaves that out the parameterless base constructor is called. Fields with initialisers are set first. A Shared Sub New has no parameters and no accessibility.
Cleanup. A class that owns resources implements IDisposable.Dispose and is used in a Using block; Protected Overrides Sub Finalize() is the finaliser the garbage collector calls (there is no destructor).
Structures, modules and nested types
Public Structure Point
Public X As Integer
Public Y As Integer
End Structure
Public Module Helpers
<Extension()>
Public Function Shout(text As String) As String
Return text.ToUpper() & "!"
End Function
Public Const Version As String = "1.0"
Public Counter As Integer
End Module
Public NotInheritable Class Outer
Private Class Hidden
End Class
Friend Class Inside
Protected Friend Sub M()
End Sub
End Class
End Class
- Structure: a value type: copied on assignment, never
Nothing(apart fromNullable), cannot inherit or be inherited but can implement interfaces. It may hold fields, properties, methods, events, operators and constructors with parameters; the default (zeroing) constructor always exists. - Module: a standard module is a class that cannot be created or inherited and whose members are all shared implicitly. Its members can be used by simple name anywhere in the namespace (and, when imported, elsewhere), which is why
Sub Mainand helper functions live in modules. Modules cannot implement interfaces. - Nested types: a type may be declared inside a class, structure or interface (and a module), with its own accessibility.
- Attributes are written in angle brackets before a declaration:
<Serializable>,<Obsolete("use New")>,<Flags>; arguments are positional or named with:=, and several may share one pair of brackets.<Assembly: …>and<Module: …>targets apply to the whole assembly or module (see file structure).
Interfaces and delegates
Public Interface IPet
Inherits IAnimal
Property Owner As String
End Interface
Public Delegate Function Handler(value As Integer) As Boolean
Public Delegate Sub Notifier(message As String)
Public Class Dog
Inherits Animal
Implements IPet
Public Property Owner As String Implements IPet.Owner
End Class
An Interface declares signatures of methods, properties and events (no bodies, no fields) and may inherit several other interfaces. A class names the interfaces it implements in Implements and says which member implements which with a trailing Implements Interface.Member clause (a member can implement several). A Delegate Sub or Delegate Function declares a delegate type: a reference to a method with that signature (see delegates).
Not described
Known, not yet covered
- Reference: the full rules of shadowing and name hiding, interface re-implementation, variance, the accessibility checking of constituent types, default instances of forms (
My.Forms) and the rules for default constructors of structures are in the general-concepts and type chapters of the specification and are described only in outline. - Reference: records, init-only properties (
Initaccessors are only consumed, version 16.9), required members and other C# 9 to 12 type features are not available in Visual Basic and are not described.
See also
- Procedures and events – the members of a type
- Generics, lambdas and delegates – generic types and constraints
- The standard library and My – the base class library