you're reading...

.NET OOP (Shared Class or Member Class)

Shared Variables

There is another type of shared member we can create. There are times when it is nice to 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 MyCounter
Private Shared mintCount As Integer
End Class

As with shared methods, we can scope the shared variable as required. Where Shared methods are Public by default, Shared variables are Private by default.

In general, it is good practice to always explicitly define the scope of methods and variables to avoid confusion.

The important thing about shared variables is that they are common across all instances of the class. We could enhance our class slightly as follows:

Public Class MyCounter
Private Shared mintCount As Integer
Public Sub New()
mintCount += 1
End Sub
Public ReadOnly Property Count() As Integer
Return mintCount
End Get
End Property
End Class

As we create each instance of the class the counter is incremented by one.

The += operator is new to VB.NET and is covered in Chapter 3.

At any point, we can retrieve the count value via the Count property. Thus, if we run the following client code we’ll get a resulting value of 3:

Protected Sub Button4_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim obj As MyCounter
obj = New MyCounter()
obj = New MyCounter()
obj = New MyCounter()
MsgBox(obj.Count, MsgBoxStyle.Information, “Counter”)
End Sub

If we run it again we’ll get 6, then 9, and so forth. As long as our application is running the counter will remain valid. Once our application terminates the counter also goes away.

This technique can be very useful for server processes that run “forever” since they can keep usage counters or other values over time very easily. The values are only reset when the process is restarted.

Other Sample

Public Class MyNetworkInfo 
 Private Shared userIpAddress As string 
 Public Sub New(byVal ip) 
 userIpAddress = ip 
 End Sub 
 Public Sub New() 
 End Sub 

 Public ReadOnly Property getUserIpAddress() As String 
 Return userIpAddress 
 End Get 
 End Property 
End Class 

assing value and calling the class

with new myNetworkInfo("localhost")
end with 
with new myNetworkInfo()
end with

result is “locahost”


About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 10 other followers

December 2011
« Nov   Jan »


Web Statistic

Blog Stats

  • 160,987 hits
%d bloggers like this: