I am confused with Static
implementation in VB.NET. In C#, we can create Static class and static methods to write utility methods for our application.
Now, VB.NET lets us create Module
in place of static class. If we create a method in the module, by default it becomes static. But in my application, I have written the below code:
Public Class Utility Public Shared Function GetValue() As String // My code End Function End Class
By writing the code, I am able to access the utility method as Utility.GetValue()
. As this is not a static class, I am supposed to instantiate an object of it. But this method is available for both the class and objects of Utility
Now my questions are:
I tried consulting multiple articles, but nowhere found this exact answers. Please help.
A VB.NET module is a static class. The compiler handles this for you. Every method and property on it is static ( Shared ). A class with a static (Shared) member on it is exactly that: a class with a static (Shared) member.
Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.
A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance).
A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.
A VB.NET module is a static class. The compiler handles this for you. Every method and property on it is static
(Shared
).
A class with a static (Shared) member on it is exactly that: a class with a static (Shared) member. You don't have to create an instance of it to access the static (Shared) method, but you do to get to any of its instance members.
You can also define a Sub New()
in a module, and it becomes the static constructor for the module. The first time you try to invoke a member on the module, the static constructor will be invoked to initialize the static class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With