shared? protected? friend?

Discussion in 'ASP .Net' started by Guest, Nov 29, 2004.

  1. Guest

    Guest Guest

    hello there,

    what is the difference between Shared and Protected
    Shared? where can I read more about theses kind of
    variables (or whatever they are....sorry, don't know the
    word in eng.)

    Thanks.
     
    Guest, Nov 29, 2004
    #1
    1. Advertising

  2. Guest

    Kumar Reddi Guest

    protected is the access modifier. By default variables are private, if you
    dont give them any access modifier. So when you say protected, you are
    making those variables or method accessible only to the current class and
    its derived classes. Now about the shared, a shared variable or method is
    class level, not instance level. When you declare a variableor method
    shared, you do not need to create an instance prior to accessing that
    variable or method, you could simply say, ClassName.Variable. Therefore when
    you say protected shared, except the current class and its derived class, no
    one can see it and access it with the above mentioned syntax.

    HTH
    --
    Kumar Reddi
    http://kumarreddi.blogspot.com


    <> wrote in message
    news:a47301c4d609$d9f1aac0$...
    > hello there,
    >
    > what is the difference between Shared and Protected
    > Shared? where can I read more about theses kind of
    > variables (or whatever they are....sorry, don't know the
    > word in eng.)
    >
    > Thanks.
     
    Kumar Reddi, Nov 29, 2004
    #2
    1. Advertising

  3. > what is the difference between Shared and Protected
    > Shared?


    Shared (VB) and internal (C#) limits access to the current assembly. Types
    in other assemblies cannot access an internal type.

    Protected Shared (VB) and protected internal (C#) limits access to the
    current assembly or types derived from the class.

    Anders Norås
    http://dotnetjunkies.com/weblog/anoras/
     
    Anders Norås [MCAD], Nov 29, 2004
    #3
  4. Guest

    Karl Seguin Guest

    I think you'll find answers to most of your questions here:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/methodscope.asp


    Protected means that only the class itself and classes which inherit from it
    can call the member (member being a field/property/method). Friend means
    that any code within the same assembly (dll/project) can call the member .
    These are frequently called access modifiers. The other ones are public
    (anyone can call it) and private (only the class itself can call it). You
    can also have a mix of protected friend, meaning only the class itself,
    classes which inherit from it, or classes within the same DLL can call the
    member .

    Shared is something entirely different. It denotes that a member doesn't
    apply to an instance of a class. For example, given:

    public class Test
    public function Add(number1 as integer, number2 as integer) as integer
    return number1 + number2;
    end function

    public shared function Multiply(number1 as integer, number2 as integer)
    as long
    return number1 * number2;
    end function
    end class


    You can call the 2nd one (not shared) simply by doing:
    dim value as long = Test.Multiply(1,2)

    to call the 1st one, you need an instance of Test:

    dim t as new Test()
    dim value as integer = t.Add(1,2)


    note how the 1st is done on an instance (t) of Test, whereas the 2nd one is
    simply called on the class.

    Karl

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


    <> wrote in message
    news:a47301c4d609$d9f1aac0$...
    > hello there,
    >
    > what is the difference between Shared and Protected
    > Shared? where can I read more about theses kind of
    > variables (or whatever they are....sorry, don't know the
    > word in eng.)
    >
    > Thanks.
     
    Karl Seguin, Nov 29, 2004
    #4
  5. Hi,

    Shared and protected / friend are actually quite different. Shared can
    prefix a class method - indicating the method is associated with the class
    itself, not an instance of the class - see
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyShared.asp

    Protected / friend denote accesibility - see
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyProtected.asp
    Protected exposes the method or property internally and to any
    extensions of the class within which it is declared. Friend exposes the
    method to an all-together different set of objects.

    See
    http://msdn.microsoft.com/library/d...us/vbcn7/html/vbconCreatingYourOwnClasses.asp
    for a good overview.

    So I might have

    Class Aeroplane
    Private _WingStress as integer
    Protected EngineSize as integer
    Public Sub Climb(degree as integer) ...
    Public Shared Readonly Property SpeedOfSound as integer
    End class

    Class 747 : Inherits Aeroplane
    Public Sub New()
    EngineSize = 500
    _WingStress = 75 'would cause an error because _WingStress is
    private and can't be accessed in an extension of Aeroplane
    End Sub
    End class

    And from somewhere totally separate I could do the following

    response.write ("The Speed of Sound is :" & Aeroplane.SpeedOfSound)

    And BTW you could quite easily have found this information yourself -
    try typing "shared" "protected" and "friend" into the search box on
    msdn.microsoft.com

    Apologies for the bad example - I'm sure you can quickly find a much
    better illustration of the point.

    Regards,

    Nick




    <> wrote in message
    news:a47301c4d609$d9f1aac0$...
    > hello there,
    >
    > what is the difference between Shared and Protected
    > Shared? where can I read more about theses kind of
    > variables (or whatever they are....sorry, don't know the
    > word in eng.)
    >
    > Thanks.
     
    Nick Stansbury, Nov 29, 2004
    #5
  6. Guest

    Scott Allen Guest

    Anders:

    Shared and internal are not equivalent. The internal keyword is an
    access modifier. I believe the VB.NET equivalent keyword is Friend.
    The Shared (VB) and static (C#) keywords indicate if a member is an
    instance member or a member of the type.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 29 Nov 2004 13:14:02 +0100, "Anders Norås [MCAD]"
    <> wrote:

    >> what is the difference between Shared and Protected
    >> Shared?

    >
    >Shared (VB) and internal (C#) limits access to the current assembly. Types
    >in other assemblies cannot access an internal type.
    >
    >Protected Shared (VB) and protected internal (C#) limits access to the
    >current assembly or types derived from the class.
    >
    >Anders Norås
    >http://dotnetjunkies.com/weblog/anoras/
    >
     
    Scott Allen, Nov 29, 2004
    #6
    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. Andreas Klemt
    Replies:
    2
    Views:
    582
    Andreas Klemt
    Jul 5, 2003
  2. dw

    Friend & Protected keywords

    dw, May 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,879
  3. MiG
    Replies:
    12
    Views:
    638
    Siddharth Jain
    Aug 12, 2007
  4. Rahul
    Replies:
    1
    Views:
    920
    Rahul
    Apr 7, 2008
  5. Peter
    Replies:
    2
    Views:
    296
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page