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