What is the difference between a module and a class?

A class is more of a unit, and a module is essentially a loose collection of stuff like functions, variables, or even classes.

In a public module, classes in the project have access to the functions and variables of the module. You don't have to specify the module name to address one. You can also have classes in a module.

The variables and functions of a class are under tighter "ownership" by the class. Public variables and functions (methods) used by other classes are used with the classname: classname.method, unlike those in modules.

There is only one instance of a module, but a one or more instances of a class can be used at once.


A class is a type. You can use this type like any other type (String, Integer, Date, FileInfo ...) to declare variables, parameters, properties, and function return types.

Let us make a little example:

Public Class Person
    Public Property FirstName As String
    Public Property LastName As String

    Public Overridable Sub Print() 'I will explain Overridable later.
        Console.WriteLine($"{FirstName} {LastName}")
    End Sub
End Class

Now you can declare variables of type Person

Dim sue, pete As Person
Dim persons As List(Of Person)

sue = New Person()
sue.FirstName = "Susan"
sue.LastName = "Miller"

pete = New Person()
pete.FirstName = "Peter"
pete.LastName = "Smith"

persons = new List(Of Person)()
persons.Add(sue)
persons.Add(pete)

For Each person As Person In persons
    person.Print()
Next

Whereas modules are static. I.e. Data stored in a module exists exactly once. On the other hand, you do not have to instantiate a module with New, therefore they are often used to store global data and for methods that are available globally. For instance, you could store the persons list in a module.


But there is much more you can do with classes. You can derive a class from a base class. This new class inherits everything from the base class and can add more stuff to it. For instance, you could derive an Employee class from Person

Public Class Employee
    Inherits Person

    Public Property Salary As Decimal

    Public Overrides Sub Print
        Console.WriteLine($"{FirstName} {LastName}, Salary = {Salary}")
    End Sub
End Class

The Overridable keyword in Person.Print allows deriving classes to re-define (to override) the Print method. (Functions and Subs in classes are called methods.)

Employees are assignment compatible to Persons. You could add an employee to the persons list. This does not require any change in the For Each loop, i.e., the call of person.Print() automatically calls the right Print method (the first one for "normal" persons and the second one for employees).

Dim emp as Employee

emp = New Employee()
emp.FirstName = "Frank"
emp.LastName = "Taggart"
emp.Salary = 3500.00D

persons.Add(emp)

There is much more to say about classes. I hope that you got a certain idea of what you can do with classes.

See Objects and classes in Visual Basic and especially the section Differences between classes and modules.


A module is nothing more than another name for a static class.
That's all there is to it.
If you don't believe it, compile a module with VB.NET and decompile with ILSpy using C-Sharp.

And yes, that means you're correct, in terms of functionality, a module is a SUBset of a class.
It follows, that in terms of functionality, a class is a SUPERset of a module, because it can contain static as well as non-static methods & variables, as well as the virtual and protected access modifiers.


The main difference between classes and modules is that classes can be instantiated as objects while standard modules cannot. Because there is only one copy of a standard module's data, when one part of your program changes a public variable in a standard module, any other part of the program gets the same value if it then reads that variable. In contrast, object data exists separately for each instantiated object. Another difference is that unlike standard modules, classes can implement interfaces.

Source: http://msdn.microsoft.com/en-us/library/7825002w(en-US,VS.80).aspx