Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a module and a class?

Tags:

When adding a new file to a VB.Net project in Visual Studio, I'm given the option of both a 'Class' and a 'Module'. A class is described as

An empty class file 

While a module is described as

A file for storing groups of functions 

This seems to imply that a module is less useful that a class, since a class can store groups of functions and more.

Is it the case that a module is simply a group of functions, or is there more to the module than the visual studio documentation suggests?

like image 279
Stephen Avatar asked Jan 27 '13 15:01

Stephen


People also ask

What is difference between module and class in python?

A module can have zero or one or multiple classes. A class can be implemented in one or more . py files (modules). But often, we can organize a set of variables and functions into a class definition or just simply put them in a .

What is the difference between module and class module in VBA?

In languages such as C# and Java, classes are used to create objects. Class Modules are the VBA equivalent of these classes. The major difference is that VBA Class Modules have a very limited type of Inheritance* compared to classes in the other languages.

What is the difference between a module and an object?

A program module is a self-contained independent program segment only it does not provide security to data whereas an object is a collection of data members and member functions that operate on data and data is provided with security.

Why do we use Modules in class?

Modules allow instructors to organize content to help control the flow of the course. Modules are used to organize course content by weeks, units, or a different organizational structure.


2 Answers

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.

like image 105
Olivier Jacot-Descombes Avatar answered Sep 29 '22 10:09

Olivier Jacot-Descombes


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.

like image 45
Stefan Steiger Avatar answered Sep 29 '22 10:09

Stefan Steiger