Chapter 10. Modules

Visual Basic programming language provides a simplified way for working with shared classes. Modules have been a part of Visual Basic since the beginning, so it is the real reason why Visual Basic .NET has modules versus C# which has shared classes. In this brief chapter you learn about the module feature.

Modules Overview

Modules are a specific feature of the Visual Basic programming language. You can think of modules as classes exposing only shared members; each module is defined within a Module..End Module code block. The following code provides an example of a module:

Module Module1

    Sub Main()
        'DoSomething is a method defined elsewhere
        DoSomething()
    End Sub

End Module

Differently from Visual C#, Visual Basic does not directly support shared classes; it provides support for classes with only shared members. According to this, the preceding module is the Visual Basic representation of the following class:

Class Program
    Shared Sub Main()
        'DoSomething is a method defined elsewhere
        DoSomething()
    End Sub
End Class

If you had any other member in this class, it should be marked as Shared. Modules are particularly useful when you want to implement objects and members that can be easily shared across your application and that do not need an instance.

Multiple modules can be defined in one code file, or multiple code files can define one or more modules as well. The following module defines a field and a property that you can reach from anywhere in your project:

'From anywhere in your project you'll be able
'to reach the two objects
Module Declarations

    Friend myFirstField As String
    Friend Property myFirstProperty As Integer

End Module

Because you cannot create an instance of a module, you have just one copy in memory of both the field and the property. The following example instead defines a method that assigns variables defined inside a different module:

Module Methods
    Friend Sub DoSomething()
        myFirstField = "A string"
        myFirstProperty = 0
    End Sub
End Module

It is worth mentioning that, different from classes, you do not need to invoke methods by writing first the name of the class that defines methods. For example, if the DoSomething method were defined within a class named Program, you should use the following syntax:

Program.DoSomething()

With modules, this is not necessary—you simply need to invoke the method name:

DoSomething()

This is true unless a conflict exists with the method name and a local method of the same name.

Scope

Typically, modules are required within a project and are not exposed to the external world. Because of this, the default scope qualifier for modules is Friend. But because of their particular nature, modules are also allowed to be Public—but neither Private nor Protected/Protected Friend. Members defined within modules can be also marked as Private.


About Public Modules

There is a particular exception to the previous discussion: creating a custom extension methods library. Because in Visual Basic you define extension methods within modules, if you want to export such methods, you need to mark them as Public. Extension methods are discussed in Chapter 20, “Advanced Language Features.”


Differences Between Modules and Classes

There are some differences between modules and classes. This brief section looks at those differences.

No Constructor

As previously mentioned, modules can be considered as shared classes, although Visual Basic provides support for classes with only shared members and not direct support for shared classes. Because of their shared nature, as a general rule modules do not support the constructor (Sub New). An exception is that you can declare a Sub New in a module that will be private and any code in the private constructor will be executed the first time any call to the module is made.

No Inheritance Support

Modules cannot inherit from other modules or classes or be inherited. Therefore, the Inherits, NotInheritable, and MustInherit keywords are not supported by modules.


Inheritance and Shared Members

Although it is not a good programming practice, it is legal to create a NotInheritable Class that exposes only shared members.


No Interface Implementation

Modules cannot implement interfaces. If you need to implement interfaces, you should consider developing a class with shared members instead of a module.

Summary

Modules are an alternative for working with shared members and data. Although they have some limitations compared to classes, they are useful for exchanging information across the project. In this chapter you got an overview of modules and saw the differences between modules and classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.186.83