Public Shared Function v.s. Public Function

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hello,

I'm just getting started with VB and am new to the group,
so please excuse what may seem to be a rudimentary
question.

I've been writing basic programs and have noticed that the
syntax for some of these functions have been set up with
the keywords "Public Shared Function" (for example, the
Trim function) and others are set up just as "Public
Function" (for example, the IsNumeric function).

Before I came here, I tried to answer my own question,
thinking that the reason for the "Shared" keyword was that
the function would be available no matter where I went in
VB. But it works this way for both types.

Why do some functions include the "shared" keyword, and
others do not?

Thanks in Advance,
Chris
 
Shared means that it is "shared" accross all instances of a class and
therefore, you don't need any particular instance to use it.

Here's an example:

The System.Windows.Forms.MessagBox class is very popular in Windows
applications. To use it, you just type:
MessageBox.Show(message, etc.)
But, you don't have to create an instance of the class to use it first (Dim
MessageBox as New MessageBox). You can just call the Show method of the
MessageBox class because the Show method is declared "shared".

With properties that are declared as "shared", it means 2 things. One is
that the value of the property is shared accross all instances of the class,
so take this example:

Dim x as New Foo
x.SomeSharedProperty = 17

Dim y as New Foo
MessageBox.Show(y.SomeSharedProperty)

What will the MessageBox wind up showing? You guessed it, 17! The 2
instances of the class "share" the value of the shared property. If on
instance of the class changes the property value, then the value changes for
all of the instances of the class.

Now the second meaning of shared on a shared property is the same as
described in the first example. You don't even need an instance of the
class to access the property. So, I could have set the SomeSharedProperty
value to 17 on the "type" Foo, without having to create an instance of it.

Lastly, the keyword "Public" just means that the element will be available
outside of the class module it was created in.

Hope that helps.

Scott M.
 
* "Chris said:
I'm just getting started with VB and am new to the group,
so please excuse what may seem to be a rudimentary
question.

I've been writing basic programs and have noticed that the
syntax for some of these functions have been set up with
the keywords "Public Shared Function" (for example, the
Trim function) and others are set up just as "Public
Function" (for example, the IsNumeric function).

Before I came here, I tried to answer my own question,
thinking that the reason for the "Shared" keyword was that
the function would be available no matter where I went in
VB. But it works this way for both types.

Why do some functions include the "shared" keyword, and
others do not?

\\\
Public Module Foo
Public Sub Bar()
End Sub
End Module
///

.... is ~ the same as:

\\\
Public Class Foo
Public Shared Sub Bar()
End Sub
End Class
///

Methods defined in modules are shared by default. You can call the
methods without having an instance of 'Foo', just by typing 'Foo.Bar()'.
Procedures defined in a module can be even accessed without mentioning
the name of the module containing them ('Bar()').
 
Hi Chris,

You got all "how" answers,

Here a "why"

A non shared function can used as often as you wants, as a new procedure and
all "in" the class declared values or objects are always new.

A sample, I find it alway a chalenge to use the class I am using in my own
procedure. Here is a sample.
\\\
Private Sub Load_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim nada As New doCtr(Me)
End Sub
End Class
Public Class doCtr
Public Sub New(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Text = "CTR"
Dim newdoCtr As _
New doCtr(ctr)
Next
End Sub
End Class
///

A shared class can only used once in a project (it is always there). Sounds
strange but if you give it every time the needed values you can use it
hundreds times of course.

I hope this helps,

Cor
 
Back
Top