Question about declaring Public Function as 'Shared'

S

Schizoid Man

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
 
K

Kerry Moorman

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
 
H

Herfried K. Wagner [MVP]

Schizoid Man said:
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'.
 
S

Schizoid Man

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


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'.

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
 
H

Herfried K. Wagner [MVP]

Schizoid Man said:
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?

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
///
 
P

Phill W.

Schizoid said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top