Access modifier: Special 'Protected' behavior?

A

Armin Zingler

Hi,

my understanding of access modifiers has always been that they specify
_from where_ the member can be accessed. I don't know any other constraints
about this. So:

- Private: Accessible _from_ the same class only
- Friend: Accessible _from_ the same assembly
- Public: Accessible _from_ everywhere
- Protected: Accessible _from_ the same class and inherited classes.

(combinations like "Protected Friend" not of interest here)

Right? Now I figured out that this only criterion does not apply
to 'Protected'. Example:

Class Class1
Protected var As Integer
End Class

Class Class2
Inherits Class1

Sub bla()
Dim o As New Class1

var = 1
o.var = 2 'ERROR: not accessible becaus Protected

End Sub
End Class

IMO, the error is not ok. The member is definitely accessed _from_
an inherited class. This, as the documentation clearly states, is the
condition that must be met. Even as it is met, the member can
not be accessed.

As 'var = 1' is the same as 'Me.var = 1', the only difference between

Me.var = 1

and

o.var = 1

is the 'kind' of reference used to access the member. One 'kind' is "Me", the
other kind is "any other reference". I remember, there was a distiction between
these two 'kinds' in VB6 [foot note #1], but I could have sworn that this has
changed in .Net and the _only_ criterion was the location of the member access.

Well, as the compiler works this way: Is this documented? And why is there
a break with 'Protected' compared to the others? I can't find this exception
in the documentation.

The inconsistence of this behavior is also shown if you change it to

Dim o As New Class2

Now it works. I think it must not work, too, because, be it a Class1 or a Class2
reference, it is the _same_ member accessed from the _same_ location.


Armin

[1] not with inheritance of course, but 'Private' members in VB6 could only
be accessed from the same instance, not even from other instances of the
same class. You even had to write "var = 17" because "Me.var = 17" didn't work
even as it should have been the same.
 
T

Thomas Scheidegger

?Hi Armin
Dim o As New Class1
o.var = 2 'ERROR: not accessible becaus Protected
IMO, the error is not ok.

IMHO it's OK, read:

http://msdn.microsoft.com/en-us/library/e8zxe4y5.aspx
Protected...
"You can read or write to the variable from anywhere within the class,
as well as from _WITHIN_ any class derived from it,
but not from _OUTSIDE_ any class in the derivation chain.

and C#:
http://msdn.microsoft.com/en-us/library/bcd5672a.aspx
"A protected member of a base class is accessible in a derived class
only if the access occurs through the derived class type"
 
A

Armin Zingler

Am 07.10.2010 02:40, schrieb Thomas Scheidegger:
?Hi Armin


IMHO it's OK, read:

http://msdn.microsoft.com/en-us/library/e8zxe4y5.aspx
Protected...
"You can read or write to the variable from anywhere within the class,
as well as from _WITHIN_ any class derived from it,
but not from _OUTSIDE_ any class in the derivation chain.

Class2 is _IN_ the derivation chain. It inherits from Class1.

See also:
http://msdn.microsoft.com/en-us/library/76453kax.aspx

"....can be accessed only from within the same class, or from a class
derived from this class"

The condition is clearly met.
and C#:
http://msdn.microsoft.com/en-us/library/bcd5672a.aspx
"A protected member of a base class is accessible in a derived class
only if the access occurs through the derived class type"

The access occurs through Class2. Class2 is a derived type.


As I said, the only difference is the kind of reference used.
That this kind makes the distinction isn't mentioned anywhere.
I can't take it from your links either.
 

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

Top