scope of protected friend modifier

pavansagar

Member
Joined
Jan 11, 2006
Messages
8
Programming Experience
Beginner
To my knowledge, Friend is accessible throughout the project and protected is accessible in the base class and all its derived classes. So protected friend means it should be accessible throughout the project and in the base class and in all derived classes (these may be in any other project).
Here I created classlibrary of name classlibrary1 and it contains the following code.
Public Class base1
Friend a As Integer = 1
Protected b As Integer = 2
Protected Friend c As Integer = 3
Protected Friend Overridable Sub display() 'error here
Console.WriteLine("protected friend--in base1 class in another project")
End Sub
End
Class

Now I compiled it and imported this classlibrary1.dll into my current project by adding reference. In this the, method display () which is declared as protected friend is overridden. But it is showing the error as “protected overrides sub display () cannot override protected overridable sub display () because they have different access levels.” But 'c' isalso a variable which is declared as 'protected friend'. It is accessible in the current project..so why I am not able to access the display() method in the current project.

This is the code which I given in the current project.
Imports ClassLibrary1
Public Class firstderived
Inherits base1
Protected Friend Overrides Sub display()
Console.WriteLine("protected friend--in first derived class ")
End Sub
End Class

Module Module1
Sub Main()
Dim obj1 As base1 = New base1
Dim obj2 As New firstderived
obj1.display() 'errror here
obj2.display()
End Sub
End
Module

 
I just pasted your code into my IDE and I get a slightly different story. I get the following error:

'Protected Friend Overrides Sub display()' cannot override 'Protected Friend Overridable Sub display()' because it expands the access of the base method.

on this line:

Protected Friend Overrides Sub display()

This is quite reasonable as by overriding the method in the referencing assembly you are exposing a method that is declared as protected in the referenced assembly. Protected Friend means it is accessible in other assemblies but only within the derived class. By overriding it you are exposing it outside the derived class which is not allowed.

I also get this error:

'ClassLibrary1.base1.Protected Friend Overridable Sub display()' is not accessible in this context because it is 'Protected Friend'.

on this line:

obj1.display()

which is also quite reasonable because you are trying to directly access a protected member in an assembly other than the one in which it was declared.
 
Back
Top