Shared question

  • Thread starter Thread starter Rexel
  • Start date Start date
R

Rexel

What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??
I don't get it.
 
Everything in VB is a class, even a module is a special class. Why
instantiate when you dont need to?. for example, why would you need to
instatiate the function Math.Min , you only need one. Also shared
members can be threadsafe.


OHM
 
Consider following code that will count how many instances you've created:
Public Class TestMe
Private Shared _instancesCreated As Integer = 0

Public Shared ReadOnly Property InstancesCreated() As Integer
Get
Return _instancesCreated
End Get
End Property

Public Sub New()
_instancesCreated += 1
End Sub
End Class

To use it:
Dim a As New TestMe
Dim b As New TestMe
MessageBox.Show(TestMe.InstancesCreated.ToString()) 'Will display 2

Static/Shared members should contain functionality regarding the class it's
implemented on.

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
* Rexel said:
What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??

You can use a module too, but a module doesn't allow you to specify
instance members. Have a look at the design of the .NET Framework class
library to find out when shared methods make sense.
 
Rexel said:
What is the point of having a shared method in a class?

it allows you to provide functionality to users of that Class
without them having to instantiate it first.

Consider the Format() method of the String Class.

Dim s as String _
= String.Format( "{0} {1}", "Hello", "World" )
' try it if you've not come across this before...

You can quite happily use this method of the String class, even
though you haven't said "New /anything/" in your code.

HTH,
Phill W.
 
Rexel,
In addition to the other comments.

IMHO the Primary reason is Encapsulation!

Remember Encapsulation is one of the top 3 tenants of OOP. The other 2 being
Inheritance and Polymorphism.


Adding a method or field as Shared to a class, indicates that the method is
PART of that class, making it Shared indicates that it can be used without
an Instance of that class, for example Class Factories, if I want to control
the number of instances of a class

Consider the following where we only want to allow creating an instance of
the Underwriter class when we have a new unique ID.

Public Class Underwriter

Private Readonly m_hash As New HashTable

Public Shared Function FromID(ByVal id As String) As Underwriter
If m_hash.Contains(id) Then
Return DirectCast(m_hash(id), Underwriter)
Else
' code to find data on a Underwriter
Dim uw As New Underwriter(id, ...)
m_hash.Add(id, uw)
Return uw
End If
End Function

Private Sub New(ByVal id As String, ...)
...
End Sub

End Class

The Underwriter.FromID method is a factory method, it will return a new
Underwriter object from the specified ID, if the id does not already exist,
it knows how to go about creating a new Underwriter object, possible with a
database call. It is a shared function, as only the Underwriter class should
now how to create Underwriter objects & you should not need to create an
Underwriter object, just to look up a new one. The contractor is private to
prevent other modules from creating Underwriter objects.

If you had MyModule.CreateUnderwriter in a module then the constructor of
Underwriter would need to be Public or Friend, which would allow methods
other then MyModule.CreateUnderwriter to call the constructor, bypassing
MyModule.CreateUnderwriter method.

However seeing as Underwriter.FromID is shared & the constructor is private
all the Underwriter logic is neatly encapsulated in the Underwriter class!
There is no bypassing the Underwriter.FromID contract!

Hope this helps
Jay
 
From help. One sample class.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Any instance members are not guaranteed to be
thread safe.

OHM
 
OHM,
That specific class's static/shared methods are thread safe.

It does not mean that all static/shared methods on any class are thread
safe.

I will see if I can come up with a sample or any other info that they are
not thread safe.

Hope this helps
Jay
 
OHM,
Consider this sample:

Option Strict On
Option Explicit On

Imports System.Threading

Public Class ThreadMania

Private Shared m_count As Integer

Private Shared Sub Start()
Do Until m_count > 100
m_count += 1
Debug.WriteLine(m_count,
Thread.CurrentThread.GetHashCode().ToString())
Loop
End Sub

Public Shared Sub Main()
Dim t1 As New Thread(AddressOf Start)
Dim t2 As New Thread(AddressOf Start)
t1.Start()
t2.Start()
End Sub

End Class

Both threads are executing at the same time, causing both threads to update
the shared m_count variable, the resultant output is a number that jumps
between thread 1 & thread 2...

Unfortunately I cannot find references that state that static/shared methods
are not thread safe.

Hope this helps
Jay
 
Rexel,
In addition to the other comments.

IMHO the Primary reason is Encapsulation!

Remember Encapsulation is one of the top 3 tenants of OOP. The other 2 being
Inheritance and Polymorphism.


Adding a method or field as Shared to a class, indicates that the method is
PART of that class, making it Shared indicates that it can be used without
an Instance of that class, for example Class Factories, if I want to control
the number of instances of a class

Consider the following where we only want to allow creating an instance of
the Underwriter class when we have a new unique ID.

Public Class Underwriter

Private Readonly m_hash As New HashTable

Public Shared Function FromID(ByVal id As String) As Underwriter
If m_hash.Contains(id) Then
Return DirectCast(m_hash(id), Underwriter)
Else
' code to find data on a Underwriter
Dim uw As New Underwriter(id, ...)
m_hash.Add(id, uw)
Return uw
End If
End Function

Private Sub New(ByVal id As String, ...)
...
End Sub

End Class

The Underwriter.FromID method is a factory method, it will return a new
Underwriter object from the specified ID, if the id does not already exist,
it knows how to go about creating a new Underwriter object, possible with a
database call. It is a shared function, as only the Underwriter class should
now how to create Underwriter objects & you should not need to create an
Underwriter object, just to look up a new one. The contractor is private to
prevent other modules from creating Underwriter objects.

If you had MyModule.CreateUnderwriter in a module then the constructor of
Underwriter would need to be Public or Friend, which would allow methods
other then MyModule.CreateUnderwriter to call the constructor, bypassing
MyModule.CreateUnderwriter method.

However seeing as Underwriter.FromID is shared & the constructor is private
all the Underwriter logic is neatly encapsulated in the Underwriter class!
There is no bypassing the Underwriter.FromID contract!

Hope this helps
Jay

Thanks for the reply, and thanks to everyone who responded.
 
Yes, I know Jat, that's why my post said ( 'can' be thread safe )

Regards - OHM

OHM,
That specific class's static/shared methods are thread safe.

It does not mean that all static/shared methods on any class are
thread safe.

I will see if I can come up with a sample or any other info that they
are not thread safe.

Hope this helps
Jay

One Handed Man said:
From help. One sample class.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe.

OHM


OHM,
Also shared
members can be threadsafe.
No there not, where did you read that?

Jay

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in
message Everything in VB is a class, even a module is a special class. Why
instantiate when you dont need to?. for example, why would you need
to instatiate the function Math.Min , you only need one. Also
shared members can be threadsafe.


OHM



Rexel wrote:
What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??
I don't get it.
 
I see your point Jay,

Thanks.

Regards - OHM

OHM,
Consider this sample:

Option Strict On
Option Explicit On

Imports System.Threading

Public Class ThreadMania

Private Shared m_count As Integer

Private Shared Sub Start()
Do Until m_count > 100
m_count += 1
Debug.WriteLine(m_count,
Thread.CurrentThread.GetHashCode().ToString())
Loop
End Sub

Public Shared Sub Main()
Dim t1 As New Thread(AddressOf Start)
Dim t2 As New Thread(AddressOf Start)
t1.Start()
t2.Start()
End Sub

End Class

Both threads are executing at the same time, causing both threads to
update the shared m_count variable, the resultant output is a number
that jumps between thread 1 & thread 2...

Unfortunately I cannot find references that state that static/shared
methods are not thread safe.

Hope this helps
Jay

One Handed Man said:
From help. One sample class.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe.

OHM


OHM,
Also shared
members can be threadsafe.
No there not, where did you read that?

Jay

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in
message Everything in VB is a class, even a module is a special class. Why
instantiate when you dont need to?. for example, why would you need
to instatiate the function Math.Min , you only need one. Also
shared members can be threadsafe.


OHM



Rexel wrote:
What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??
I don't get it.
 
OHM,
Doh! I missed the 'can'...

I was making sure that the 'can' was definitely highlighted, yea that's it.
:-)

Jay

One Handed Man said:
Yes, I know Jat, that's why my post said ( 'can' be thread safe )

Regards - OHM

OHM,
That specific class's static/shared methods are thread safe.

It does not mean that all static/shared methods on any class are
thread safe.

I will see if I can come up with a sample or any other info that they
are not thread safe.

Hope this helps
Jay

One Handed Man said:
From help. One sample class.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe.

OHM



Jay B. Harlow [MVP - Outlook] wrote:
OHM,
Also shared
members can be threadsafe.
No there not, where did you read that?

Jay

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in
message Everything in VB is a class, even a module is a special class. Why
instantiate when you dont need to?. for example, why would you need
to instatiate the function Math.Min , you only need one. Also
shared members can be threadsafe.


OHM



Rexel wrote:
What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??
I don't get it.
 
OHM,
BTW: I agree with you, shared methods *can* be thread safe, and I suspect a
fair percentage are without even trying.

Its just too easy to make them non-thread safe... introducing problems that
are really hard to track down.

So I tend to offer thread safety a little extra due diligence.

Hope this helps
Jay

One Handed Man said:
Yes, I know Jat, that's why my post said ( 'can' be thread safe )

Regards - OHM

OHM,
That specific class's static/shared methods are thread safe.

It does not mean that all static/shared methods on any class are
thread safe.

I will see if I can come up with a sample or any other info that they
are not thread safe.

Hope this helps
Jay

One Handed Man said:
From help. One sample class.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe.

OHM



Jay B. Harlow [MVP - Outlook] wrote:
OHM,
Also shared
members can be threadsafe.
No there not, where did you read that?

Jay

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in
message Everything in VB is a class, even a module is a special class. Why
instantiate when you dont need to?. for example, why would you need
to instatiate the function Math.Min , you only need one. Also
shared members can be threadsafe.


OHM



Rexel wrote:
What is the point of having a shared method in a class?
By its definition the shared method is independent of any
instance of the class it is in. So what's the point of it being
there?
It could have just been in class B instead of class A, or in
class C.
Why is it not in a module??
I don't get it.
 
Back
Top