Saratchandra MV
Saratchandra MV

Reputation: 15

What is the use of Module in VB.Net programming?

I am creating a .dll for a CAD tool I use.

After creating a new project (class library) , I got a "public class-end class" block. I wrote my main method inside that block. When I run the dll in my CAD tool, its giving me main entry point missing error.

So i come back and change the "public class-end class" ---> "Public Module-end Module".

Now its able to find the main method.

Why is this so? Ive read on these forums that we should not be using modules as much. If I were to not use modules so much, how I am supposed to make it work without the module block?

Upvotes: 0

Views: 1647

Answers (2)

djv
djv

Reputation: 15774

VB.Net doesn't have static classes like C# has. Static classes allow you to create static methods such that

class foo
{
    void bar() {}
}

Accessed in VB.Net like this

foo.bar()

If you want to create the same functionality with VB.Net you have two options, which are either using a Module

Module foo1
    Sub bar()

    End Sub
End Module
Class foo2
    Shared Sub bar()

    End Sub
End Class

However the difference is how Module and Class are scoped. Modules are globally scoped so you can call the module method outside of the originating namespace without qualifying it with the Module name, but to call the static method you need to qualify it with the class name, and even import the namespace in the case of a class with static method

bar()
foo2.bar()

But what Modules are certainly good for is Extension Methods, and the global scope actually helps here if you want to extend a class across your project

Module Extensions
    <Extension>
    Sub bar(value As String)

    End Sub
End Module
Dim s = "my string"
s.bar()

In your case, it's expecting a C# style static class with method, and since VB.Net doesn't have static classes, a Module or class with static method both satisfy that.

Upvotes: 2

David
David

Reputation: 6111

Modules come from legacy versions of Visual Basic and sort of act as the "OG" of Visual Basic code files. When Visual Basic 6 and earlier was around, Visual Basic was sort of quasi object oriented and while classes existed, it made more sense to use modules in lieu of static classes. Then when Visual Basic .NET was introduced, Microsoft made a real effort to make the programming language a true object oriented programming language.

People likely tell you to use classes over modules because modules are holdovers from the days prior to Visual Basic being a pure object oriented language. Starting from 2005 on forward, anything you could do in a module could basically be done as a static (aka shared) only class with the exception of extension methods. So by using classes over modules, you're taking a more .NET oriented approach rather than a Visual Basic first approach.

With regards to your specific problem, the issue is that your CAD tool is looking for an entry method, but cannot find one. The simple solution is to add a shared method:

Public Class MyClass

    Public Shared Sub Main()
        ' ...
    End Sub

End Class

Upvotes: 1

Related Questions