473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Public Shared Function v.s. Public Function

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
Nov 20 '05 #1
4 45057
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" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
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

Nov 20 '05 #2
* "Chris" <an*******@discussions.microsoft.com> scripsit:
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()').

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Thank you for the quick and informative response!

Joe
-----Original Message-----
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 thesyntax 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 thatthe 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
.

Nov 20 '05 #4
Cor
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
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?

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: May | last post by:
greetings, i came across a function while browsing thru the net. This function is created in a component. what i am curious to know is, is this the correct way to create a function using: Public...
4
by: Matt | last post by:
I'm trying to write a shared function that will convert all my phone numbers from format 1234567890 to (123) 456-7890. Here is my function that I wrote: Public Shared Function...
4
by: hansiman | last post by:
isn't it possible to call a public shared function that resides in another file directly from the datagrid html code?: <asp:TemplateColumn HeaderText="Password"> <ItemTemplate> <asp:Label...
23
by: Kevin | last post by:
I'd like to declare my connection string as a public shared variable, to be used throughout my ASP. Net projected (created using VB.NET 2003). Below is the line of code: Public Shared conStr As...
1
by: David Sanschagrin | last post by:
(I previously posted this problem on vb.general.discussion but I've been told that this question is more related to VB.NET than VB6 and so that I should post that here.) I'm trying to call a...
2
by: Darrel | last post by:
I'm working on an app where the ASPX pages aren't precompiled with the class.vb files I'm. This is so people can add their own ASPX pages down the road to the app (the .aspx pages become...
8
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
1
by: =?Utf-8?B?am9uZWZlcg==?= | last post by:
Is it possible to use Server.MapPath in a Public Shared Function? Using the following code in this context - shows "Sever" as not declared - Public Shared Function Back_End_State() As Integer...
3
by: Jeff | last post by:
....still new to vb.net and vs 2005 web applications. I remain confused about the "shared" variable/table designation and the difference between "public" etc. I wish to place an entire table from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.