"Henry" <ea*******@yahoo.com> wrote in message
news:25*************************@posting.google.co m...
Hi guys,
I want to write some global functions which can be called from
different asp.net page.
In Visual Basic, there is a global module which allow me to do that.
In Visual Basic .net, I have to create a class file to host these
functions.
And every time when I need to call these functions, I have to create a
object.
If I don't want to create a object every time, I need to make these
functions as shared.
However, is it true that shared function will store all local variable
in same place even when being called in multi-thread environment?
For example:
Public Class MyClass
Publich Shared Sub MySub
Dim a as integer
........
End Sub
End class
If MyClass.MySub is called by multi-thread, will it cause error
because there is only one copy of local variable "a"?
No. "a" will be local. The problem you may have heard about is the
following:
Public Class MyClass
Private Shared beVeryCareful As Integer
Public Shared Sub MySub
beVeryCareful = beVeryCareful + 1
End Sub
End Class
"beVeryCareful" will be shared by all threads. Since the modification is not
synchronized, you'll have a race condition on your hands.
Second question: in asp.net is there any other way to implement global
function, without using OOP?
Why would you not want to use OOP?
As you become more accustomed to it, you'll find that you write fewer and
fewer "global functions". I find that once I start using classes to keep
related functions together, that I begin to find relationships between the
classes. I also tend to find that these functions are operating on the same
pieces of data over and over. This makes them strong candidates for becoming
a class, where the data they manipulate becomes class member data, and each
set of data becomes an instance of the class.
John Saunders