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.