473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

public vs. shared

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
Nov 19 '05 #1
10 3654
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

Nov 19 '05 #2
> 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
Nov 19 '05 #3
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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

Nov 19 '05 #4
> 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
Nov 19 '05 #5
"darrel" <no*****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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'?


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
Nov 19 '05 #6
> 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
Nov 19 '05 #7
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?


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.
Nov 19 '05 #8
> 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
Nov 19 '05 #9
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.


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.
Nov 19 '05 #10
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: May | last post by:
greetings, i came across a function while browsing thru the net. This function is created in a component. what i am curious to know is, is this the correct way to create a function using: Public...
2
by: THY | last post by:
Hi, I am having some problem, I declare few variable in a public module and use them in the web application. But after that I found that the variable declared in public variable =...
3
by: Joe Fallon | last post by:
I have a Shared varibale in a base class and all the Shared methods in the sub-classes use it (and CHANGE it). I thought I was saving myself the "trouble" of Dimming this variable inside each...
4
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been writing basic programs and have noticed that the...
5
by: Neil Steventon | last post by:
Hi, I am new to VB.net , well the objects concept anyway and was wondering if there were any good articles / turorials on when and why you would use certain declarations such as Private test...
2
by: Jon Paal | last post by:
In a 2.0 vb code class, I have a private property and a shared public function The property value has already been passed into the class, now I am trying to use a public finction which also needs...
2
by: Darrel | last post by:
I'm working on an app where the ASPX pages aren't precompiled with the class.vb files I'm. This is so people can add their own ASPX pages down the road to the app (the .aspx pages become...
8
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.