ARTICLE

Shared keyword in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic Language June 16, 2011
This article will discuss about shared keyword in VB.NET.
 
Reader Level:

Shared keyword

A shared method is not accessed via an object instance like a regular method, but rather is accessed directly from the class. The shared keyword in VB.NET is the equivalent of the static keyword in C#. In VB.NET, the shared keyword can only be applied to methods within a class, however, in C#, the static keyword can be applied to both methods within a normal class, and also at the class level to make the entire class static.

Shared Variables

when we share a value across all instances of a class when every object of a given type should share the same variable. This is accomplished through the use of shared variables.

A shared variable is declared using the Shared keyword, much like a shared method:

Public Class hello
Private Shared roh As Integer
End Class

We have no need an instance of the class to call the method.

Such as:

Public Class Rohatash
Public Shared Sub MyMethod()
// Do something in the method
End Sub
End Class

We can call shared method without create instance of class.

MyClass.MyMethod()

For Example

Module Module1

   Public NotInheritable Class Myclass

       Private Sub New()

       End Sub

 

       Public Shared Function Add(ByVal num1 As Integer,ByVal num2 As Integer) As Integer

           Return num1 + num2

       End Function

 

       Public Shared Function [Sub](ByVal num1 As Integer,ByVal num2 As Integer) As Integer

           Return num1 - num2

       End Function

       Public Shared Function Mul(ByVal num1 As Integer,ByVal num2 As Integer) As Integer

           Return num1 * num2

       End Function

   End Class

   Sub Main()

       Console.WriteLine("Sum= " & MyMath.Add(5, 2).ToString())

       Console.WriteLine("Substraction = " & MyMath.[Sub](9, 2).ToString())

       Dim mul As Integer = MyMath.Mul(7, 2)

       Console.WriteLine("Multiply  = " + mul)

   End Sub

End Module

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor