Public and Shared aren't comparable like protected and private and public
and friend are. Those are access modifiers - they modify who has the right
to access them. Public simply means that any code can access the member
(function/property/field).
Shared which is more like a flag (something either is shared or isn't),
indicates that the member (function/property/field) doesn't behave/belong to
a specific instance of the class. So your own definitions are pretty
accurate, I simply want to make it clear that public and shared aren't
comparable. Something can be public shared, private shared, friend shared,
protected shared or simply public, private, friend or protected (then
there's protected friend, but we'll ignore that for now).
private shared means that only the class itself can access the field, a
frequent use of a private shared field is for use with singletons:
public class MyClass
private shared MyClass instance = nothing
public shared function GetInstance() as MyClass
if instance is nothing then
instance = new MyClass()
end if
return instance()
end function
private sub new()
end sub
...
end class
from the above code you can see that MyClass can never be created directly
since the constructor is private (outside code can't call it). Outside code
also can't access the instance field because it too is private. Outside
code can however access GetInstance because it's public. GetInstance checks
to see if the private field "instance" is nothing (it can access a private
field because it's all the same class), if it is, it creates the instance
(again, it can access the private constructor) and returns the instance
(there is a possible race condition, but that's besides the point).
What's neat about the above example is that GetInstance is marked shared.
IF it wasn't, no one would ever be able to call it because the constructor
is private and thus an instance can't be created. Without an instance, a
non-shared member can't be accessed.
You might find Paul Vick's great VB.Net book useful:
http://print.google.com/print?id=ejz...6Q910vyvVa9Gwg
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:OA**************@TK2MSFTNGP11.phx.gbl...
I'm still trying to sort out in my head the differences between public and
shared when referring to declaring properties or variables. This is my
understanding:
shared - akin to a 'global' variable for the application. Any other code
within the application can access it.
public - can be shared across the application if instatiated.
Does that sound about right? It seems these are more useful for methods
rather than variables. Most of the time, I imagine that I'd use shared if
I want to set a variable that other controls can see.
The other question, is what does 'private shared' mean? Is that shared but
only with in the particular class?
-Darrel