Reputation: 3573
Is there a way to call directly a method without creating instance of specific class like it is in C# so apart from that way:
Dim myclass as New ClassX
myclass.MyMethod()
is there a way to use soemthing like:
New ClassX.MyMethod
i found this way and seems to be working but not sure if its correct:
(New ClassX).MyMethod
Upvotes: 0
Views: 3271
Reputation: 22456
If your method is an instance-level method you can only access it by using an instance of the class. By using
(New ClassX).MyMethod
you implicitly create a new instance that you access only once.
An alternative is to change the method's signature and mark it as a Shared
method:
Public Class ClassX
Public Shared Sub MyMethod()
' ...
End Sub
End Class
Shared
is the VB.NET way of creating a static
method as it is called in C#. This way, you can access the method by only specifying the class name without creating an instance:
ClassX.MyMethod()
Upvotes: 5