shared? protected? friend?

G

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

Kumar Reddi

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
 
A

Anders Norås [MCAD]

what is the difference between Shared and Protected

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/
 
K

Karl Seguin

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
 
N

Nick Stansbury

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
 
S

Scott Allen

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top