473,890 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 45236
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.SomeSharedPro perty = 17

Dim y as New Foo
MessageBox.Show (y.SomeSharedPr operty)

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 SomeSharedPrope rty
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*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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.EventArg s) 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.Contr ols
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
4017
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 Shared Function name() As String Don't we need some sort of parameter passing to use the "As String"? Maybe something like this: Public Shared Function name(ByVal fname As String) i am also curious as to why we need to use "Public Shared"
4
1847
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 CheckPhone(byVal val as String) as String dim strCheckPhone as String If val = "" Then strCheckPhone = ""
4
2400
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 runat="server" Text='<%# DecryptString(DataBinder.Eval(Container, "DataItem.Password")) %>' /> </ItemTemplate>
23
8236
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 String = "Provider=Microsoft.Jet.OleDb.4.0;data source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb" I am getting an error at Public Shared: "Shared is not valid on a local variable declaration"
1
6789
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 "Public Shared" function in a dll built in VB.NET from a VB6 program. Whenever I try, I get an error saying that the function doesnt exist. I had doubts that the "Shared" in the VB.NET dll could be the problem. I tried to
2
3033
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 'templates' in a way). As such, each template has a function call at the top: <% retrieveData() %> and then within the page, wherever they want to show some text from the db, they just use one of the variables sent in the vb file:
8
2025
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: Public Class COM Private mMyProp As String Public Property MyProp() As String
1
3122
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 Dim FILENAME As String = Server.MapPath("SomeText.txt") - what can I do to fully qualify it?
3
2349
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 a DB into a datatable and then use it in a variety of different subs not unlike the code below. I'm not positive, but it seems that "shared" might also share the table across users, and this is not what I want. I only wish to be able to access...
0
9976
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.