global function without using object?

  • Thread starter Thread starter Henry
  • Start date Start date
H

Henry

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"?

Second question: in asp.net is there any other way to implement global
function, without using OOP?

Thank you.

Henry
 
As long as all the threads are in the same process, there will be 1 instance
of anything shared.

You can put your global function in a Module (don't need the word Shared in
this case). However, a Module gets compiled down to a class with all shared
functions anyway, but it might be more syntactically convenient for you to
do so. This would be a VB thing only.
 
Henry said:
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
 
Back
Top