public vs. shared

D

darrel

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
 
K

Karl Seguin

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=ej...aul+vick&pg=1&sig=ZxfWU68bu9eUj6Q910vyvVa9Gwg

Karl
 
D

darrel

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

Ah. This is where my confusion lies then. So, 'public and shared' really are
only for methods and classes?
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

Ah. Now, I do this now, but I only dim the variable:

class
dim variable as string

sub
variable = monkey
end sub
end class

When I do that, is 'variable' by default, public shared?

Obiously, what I'm struggling with is a high-level overview of these
concepts. The books I have mention these in passing, and a lot of tutorials
online dive into describing them in a level of detail way above my head at
this point.

I'm going to check out that book you suggested. Thanks!

-Darrel
 
K

Karl Seguin

no, public and shared are for functions, properties and fields (not just
methods and classes...not sure how you got that from what i said).

when you declare dim variable as string in the class scope, it uses the
default access modifier (which is private). and you didn't declare it
shared, so it isn't. in other words:

class
dim variable as string
sub
variable = monkey
end sub
end class

is the same as
class
private variable as string
sub
variable = monkey
end sub
end class


if you want something shared, you always have to implicitly say so. if you
want something private, that's the default so you don't have to say so. If
you want something public, since private is the default, you have to
specifically say so.

Karl
 
D

darrel

when you declare dim variable as string in the class scope, it uses the
default access modifier (which is private). and you didn't declare it
shared, so it isn't. in other words:

OK. That makes sense!

I'm hung up on the shared declaration. If you declare it 'private shared',
how is that different then just declaring it 'public'?

-Darrel
 
J

John Saunders

darrel said:
OK. That makes sense!

I'm hung up on the shared declaration. If you declare it 'private shared',
how is that different then just declaring it 'public'?

Shared has nothing to do with access. Shared means that the member is a
member of the class and not a member of a class instance. For example:

Public Class Foo
Public instanceMember As Integer
Public Shared classMember As Integer
End Class

Public Class Bar
Shared Sub Main
Dim aFoo As New Foo
Dim bFoo As New Foo
aFoo.instanceMember = 1
bFoo.instanceMember = 2
' At this point, aFoo.instanceMember = 1
' But:
aFoo.classMember = 1
bFoo.classMember = 2
' At this point, aFoo.classMember = 1, because classMember belongs
to the class
End Sub
End Class

There's one "instanceMember" for each instance of the Foo class, but there's
one "classMember" for all instances of the Foo class. That member is
"Shared" across all class instances.

And this is why not to use it in ASP.NET pages. A Shared page member is
shared across all copies of the page. If there are two requests for the same
page at the same time, then they would both share the same member, and
bFoo.classMember=2 would interfere with aFoo.

John Saunders
 
D

darrel

And this is why not to use it in ASP.NET pages. A Shared page member is
shared across all copies of the page. If there are two requests for the same
page at the same time, then they would both share the same member, and
bFoo.classMember=2 would interfere with aFoo.

Thanks, John.

So, it sounds like SHARED could be used if you needed to set a global
variable across all pages of the site for all users accessing said pages?

For instance, I could attach text to a shared variable called 'page footer'
and then have all pages access that since it woudl be the same for all?

-Darrel
 
J

John Saunders

darrel said:
Thanks, John.

So, it sounds like SHARED could be used if you needed to set a global
variable across all pages of the site for all users accessing said pages?

For instance, I could attach text to a shared variable called 'page
footer'
and then have all pages access that since it woudl be the same for all?

No, Darrel.

Setting a class member Shared means that the member is specific to the
class, not to a class instance. For example, a non-Shared variable has one
copy per class instance. A Shared variable has one copy for all class
instances.

A Shared Sub is a Sub for the entire class, not for any given instance of a
class. Such a Sub can only access Shared members. If it accessed an instance
member, which instance would it use?

If you're still confused about Shared, then just don't use it at all. It's
too dangerous in ASP.NET to be used if you don't understand it.

John Saunders

P.S. I have been assuming that you understand that a Page is a class, so
that my comments about Shared in a class apply to a page.
 
D

darrel

If you're still confused about Shared, then just don't use it at all. It's
too dangerous in ASP.NET to be used if you don't understand it.

That's probably the best advice!

Actually, based on your descriptoin, I don't think I really have a need for
a shared variable.
P.S. I have been assuming that you understand that a Page is a class, so
that my comments about Shared in a class apply to a page.

Yea, I'm getting the hang of classes. It's the instances of classes that are
tripping me up a bit.

-Darrel
 
J

John Saunders

darrel said:
That's probably the best advice!

Actually, based on your descriptoin, I don't think I really have a need
for
a shared variable.


Yea, I'm getting the hang of classes. It's the instances of classes that
are
tripping me up a bit.

A dog is a class. Fido is an instance of dog and so is Fifi.

John Saunders

P.S. Dog.vb is not a class, nor is it a "class file". It's a file that
happens to have a class in it.
 
K

Karl Seguin

While I agree with John that you probably shouldn't use them, I must say
that this would be an acceptable usage of shared variables (although you
shouldn't be hard-coding text in your application ;) ). Public shared has
many similarities to global variables. I just wanted to say that your
understanding here seems correct. They aren't safe to use because of
threading issues which could be very dangerous (unless they are read-only,
but then simply declare it as a constant (const), which is pretty much a
read-only shared field :) )

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top