public vs. shared

Discussion in 'ASP .Net' started by darrel, Dec 20, 2004.

  1. darrel

    darrel Guest

    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
     
    darrel, Dec 20, 2004
    #1
    1. Advertising

  2. darrel

    Karl Seguin Guest

    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



    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "darrel" <> wrote in message
    news:...
    > 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
    >
    >
     
    Karl Seguin, Dec 21, 2004
    #2
    1. Advertising

  3. darrel

    darrel Guest

    > 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
     
    darrel, Dec 21, 2004
    #3
  4. darrel

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > > 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
    >
    >
     
    Karl Seguin, Dec 21, 2004
    #4
  5. darrel

    darrel Guest

    > 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
     
    darrel, Dec 21, 2004
    #5
  6. "darrel" <> wrote in message
    news:%...
    >> 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
     
    John Saunders, Dec 22, 2004
    #6
  7. darrel

    darrel Guest

    > 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
     
    darrel, Dec 22, 2004
    #7
  8. "darrel" <> wrote in message
    news:%...
    >> 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.
     
    John Saunders, Dec 22, 2004
    #8
  9. darrel

    darrel Guest

    > 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
     
    darrel, Dec 22, 2004
    #9
  10. "darrel" <> wrote in message
    news:%...
    >> 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.
     
    John Saunders, Dec 22, 2004
    #10
  11. darrel

    Karl Seguin Guest

    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" <> wrote in message
    news:%...
    > > 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
    >
    >
     
    Karl Seguin, Dec 23, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,405
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    606
    SpamProof
    Oct 21, 2003
  3. Joe Fallon
    Replies:
    3
    Views:
    776
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  4. Kevin Spencer
    Replies:
    2
    Views:
    3,327
    Kevin Spencer
    Sep 15, 2004
  5. Steve Mauldin
    Replies:
    5
    Views:
    1,732
    Steve Mauldin
    Jan 26, 2006
Loading...

Share This Page