In Visual Basic .NET, a static method is a method that belongs to a class rather than to an instance of that class. A static method can be called directly on the class itself without creating an object of that class.
To define a static method in a class in Visual Basic .NET, you need to declare the method with the Shared keyword. Here is an example definition of a static method in a class:
Public Class MyClass
Public Shared Function StaticMethod() As Integer
' Code for the static method goes here
End Function
End Class
In this example, we have defined a static method called StaticMethod in the class MyClass. Note the use of the Shared keyword before the method definition. This indicates that the method is static.
To call this static method from elsewhere in your code, you can use the class name followed by the method name, like so:
Dim result As Integer = MyClass.StaticMethod()
In this code, we are calling the static method StaticMethod of the class MyClass, and storing the result in the variable result.
It is also possible to define static properties, which can be accessed in a similar way using the class name. Here is an example of a static property:
Public Class MyClass
Public Shared Property StaticProperty As Integer
Get
Return _staticProperty
End Get
Set(value As Integer)
_staticProperty = value
End Set
End Property
Private Shared _staticProperty As Integer
End Class
In this example, we have defined a static property called StaticProperty in the class MyClass. Note the use of the Shared keyword before the property definition, indicating that it is a static property.