By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,952 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,952 IT Pros & Developers. It's quick & easy.

shared? protected? friend?

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
<an*******@discussions.microsoft.com> wrote in message
news:a4****************************@phx.gbl...
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.

Nov 18 '05 #2

P: n/a
> 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/
Nov 18 '05 #3

P: n/a
I think you'll find answers to most of your questions here:
http://msdn.microsoft.com/library/de...ethodscope.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/
<an*******@discussions.microsoft.com> wrote in message
news:a4****************************@phx.gbl...
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.

Nov 18 '05 #4

P: n/a
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/de...akeyShared.asp

Protected / friend denote accesibility - see
http://msdn.microsoft.com/library/de...yProtected.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/de...OwnClasses.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


<an*******@discussions.microsoft.com> wrote in message
news:a4****************************@phx.gbl...
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.

Nov 18 '05 #5

P: n/a
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]"
<an**********@objectware.no> 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/


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.