Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

Question about declaring Public Function as 'Shared'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Schizoid Man - 01 Mar 2008 14:34 GMT
Why do I have to declare a Public Function as 'Shared' in a Public Class
 in order to make it visible from another class?

In this regard I don't understand how Public Function without the Shared
declaration is different from a Private Function, because I cannot call
either method from outside the class that those methods are declared in.

All classes refer to the same Namespace.

Thanks,
Schiz
Kerry Moorman - 01 Mar 2008 15:54 GMT
Schiz,

To use a non-shared public method of a class you need to create an object
from the class and call the object's method.

Kerry Moorman

> Why do I have to declare a Public Function as 'Shared' in a Public Class
>   in order to make it visible from another class?
[quoted text clipped - 7 lines]
> Thanks,
> Schiz
Herfried K. Wagner [MVP] - 01 Mar 2008 16:20 GMT
"Schizoid Man" <schiz@lon.don> schrieb:
> Why do I have to declare a Public Function as 'Shared' in a Public Class
> in order to make it visible from another class?

Place the caret on 'Shared' and press F1 :-).

> In this regard I don't understand how Public Function without the Shared
> declaration is different from a Private Function, because I cannot call
> either method from outside the class that those methods are declared in.

Shared members belong to all instances of the class and can even be used
when no instance of the class exists.  To do the latter, you can simply use
the '<type name>.<shared member>' syntax.  Instance members, on the other
hand, always belong to one instance of the type.  Thus you have to
instantiate the type in order to be able to access them:

\\\
Dim c As New Car()
c.Color = Color.Red
...
///

'Color' is a non-shared public property of the class 'Car'.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Schizoid Man - 02 Mar 2008 13:31 GMT
> "Schizoid Man" <schiz@lon.don> schrieb:
>> Why do I have to declare a Public Function as 'Shared' in a Public
[quoted text clipped - 20 lines]
>
> 'Color' is a non-shared public property of the class 'Car'.

Hello Herfried,

Thank you for the reply. If I am writing a simple utility class with
self-contained functions like Max, Min, Average that don't require
access to class members, what is the appropriate solution?

It strikes me as inefficient to first instantiate a class and then call
its respective method.

Would the correct course be to declare each function as shared?

Thanks,
Schiz
Herfried K. Wagner [MVP] - 02 Mar 2008 19:04 GMT
"Schizoid Man" <schiz@lon.don> schrieb:
> Thank you for the reply. If I am writing a simple utility class with
> self-contained functions like Max, Min, Average that don't require
[quoted text clipped - 4 lines]
>
> Would the correct course be to declare each function as shared?

Yes, this looks like an ideal scenario for shared methods.

\\\
Public NotInheritable Class Statistics
   Private Sub New()
       '
   End Sub

   Public Shared Function Min(...) As ...
       ...
   End Function

   ...
End Class
///

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Phill W. - 05 Mar 2008 13:20 GMT
> Why do I have to declare a Public Function as 'Shared' in a Public Class
> in order to make it visible from another class?

You don't.  Making it Public does that.

> In this regard I don't understand how Public Function without the Shared
> declaration is different from a Private Function, because I cannot call
> either method from outside the class that those methods are declared in.

Let's think about a couple methods found in the String class.
First, the instance (i.e. non-Shared) method, Split():

   Dim s as String = "a|b|c"

   ? s.Split( "|" ).Length
   3

In this case, it's really /quite/ important that you know /which/ string
object [instance] you're playing with.  Using Split on another string
gets you totally different results.
So, instance methods [generally] rely on instance /data/.

Now consider the Shared method, String.Join():

   Dim sArray as New String() { "a", "b", "c" }

   ? String.Join( "|", sArray )
   "a|b|c"

Now, in this case, you don't actually /care/ which String object you use
to /invoke/ this method; you're effectively calling a method that "just
happens to be" included in the String class (because it's a logical
place to put it).  That method will work perfectly happily with /any/
String value that you /pass/ to it - and that's another important
difference.  Shared methods can't use "Me" because they don't relate to
any given instance of the object.

Actually, from VB'2005 onwards, trying to use a Shared method /through/
an object instance gets a warning out of the compiler:

"123".Join( "|", sArray ) ' work up to VB'2003
String.Join( "|", sArray ) ' is the "correct" syntax.

HTH,
   Phill  W.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.