Protected Friend in Vb.Net: the field is also visible in other projects

Asked

Viewed 51 times

0

I am studying access modifiers in Vb.net and I came up with a question in Protected Friend

According to the website macoratti.net:

  • Protected: hides members of a class and "members marked with the Protected modifier can be accessed in the same class and in all classes they inherit from the class."

  • Friend: "you want all classes in your project to be able to access members of your class but classes outside of your project do not have this right.

Neither the modifier in this scenario Private nor the Protected can help you, for this situation we have the modifier Friend.

Therefore, I have two projects in the same solution, Proj1 and Proj2:

In Proj1, have the class Student:

Public Class Aluno

        Public Nome As String
        Protected Endereco As AlunoEndereco
        Protected Friend idade As Integer
        Protected Class AlunoEndereco
            Public Rua As String
            Public Cidade As String
            Public Estado As String
            Public Cep As String
        End Class
    End Class

and in Proj2 I have the Test class I inherit from Student:

Class Teste
    Inherits Aluno

    Sub New()
        idade = 2
    End Sub
End Class

As an access modifier Protected allows a member to be visible only in class and inherited classes, but when combining with the modifier protected with friend, I thought it would happen that the age camp could only be seen within the class at Proj1 but not in Proj2, but it also happened that it was possible to access age from the Test class in Proj2, could someone explain to me why this happens?

  • Some doubt is still vague...?

  • okay, all right.

1 answer

1

On the Microsoft website itself talks about the subject:

Protected Friend (Visual Basic)

Protected Friend keyword combination is a member access modifier. It confers both friend access and protected access to the declared elements, therefore they are accessible from anywhere on the same Assembly, their own class and derived classes. You can specify Protected Friend only in class members; it is not possible to apply Protected Friend to members of a structure because structures cannot be inherited. Ref. Protected Friend (Visual Basic)

When all you have is the access modifier Friend (which is the same in C# internal) is seen within the same project, but with the joining of Protected ends up opening access to other projects internally in their classes with Heritage have member visibility.

Example:

Project 1

Public Class Pasta1
    Friend P0 As String
    Protected Friend P1 As String
End Class

Project 2

Imports Prj1

Public Class Pasta2
    Inherits Pasta1
    Sub New()
        // 'Prj1.Pasta1.P0' is not accessible in 
        // this context because it is 'Friend'      
        Me.P0 = "abc"      

        Me.P1 = "abc" // Ok, está acessivel
     End Sub
End Class

I mean, that’s the difference.

Note: the context of the declaration only at class level, ie only class, excluding ai source file, namespace,interface, structures, modules and procedures.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.