Implement a static method in VB.NET

I am confused with the implementation of Static in VB.NET. In C #, we can create static classes and static methods for writing utility methods for our application.

Now VB.NET allows us to create a Module instead of a static class. If we create a method in a module, by default it will become static. But in my application, I wrote the code below:

 Public Class Utility Public Shared Function GetValue() As String // My code End Function End Class 

By writing code, I can access the utilitarian method as Utility.GetValue() . Since this is not a static class, I have to instantiate the object. But this method is available for both the class and the Utility objects.

Now my questions are:

  • Is the implementation I made that could violate any of the functions of the static class that this module provides?
  • What is the difference between this and module implementation?
  • If I create a module, will the scope be the same as this class? I want to access the method throughout the project, as well as other projects referenced by this.

I tried several articles, but did not find exact answers anywhere. Please, help.

+8
static
source share
1 answer

The VB.NET module is a static class. The compiler handles this for you. Each method and property on it is static ( Shared ).

A class with a static (Shared) member on it is exactly like this: a class with a static member (Shared). You do not need to instantiate it to access the static (Shared) method, but you do to go to any of its instances.

You can also define Sub New() in the module and become a static constructor for the module. The first time you try to call a member in a module, a static constructor will be called to initialize the static class.

+15
source share

All Articles