473,387 Members | 1,534 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,387 software developers and data experts.

shared? protected? friend?

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
5 9855
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
10
by: darrel | last post by:
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'...
4
by: John | last post by:
Hi What is the difference between a public and a shared sub? Thanks Regards
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte()...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
4
by: Eric Lilja | last post by:
Hello, consider this program: class Outer { public: class Base { friend class Outer; public: Base(int n) : n_(n) {} protected:
3
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.