Saturday, January 4, 2020
Friend and Protected Friend in VB.NET
Access modifiers (also called scoping rules) determine what code can access an elementââ¬âthat is, what code has permission to read it or write to it. In previous versions of Visual Basic, there were three types of classes. These have been carried forward to .NET. In each of these, .NET allows access only to code: Private - within the same module, class, or structure.Friend - within the same assembly.Public - anywhere in the same project, from other projects that reference the project, and from any assembly built from the project. In other words, any code that can find it. VB.NET has also added one and a half new ones. ProtectedProtected Friend The half is because Protected Friend is a combination of the new Protected class and the old Friend class. The Protected and Protected Friend modifiers are necessary because VB.NET implements the last OOP requirement that VB was missing: Inheritance. Previous to VB.NET, supercilious and disdainful C and Java programmers would belittle VB because it was, according to them, not fully object oriented. Why? Previous versions lacked inheritance. Inheritance allows objects to share their interfaces and/or implementation in a hierarchy. In other words, inheritance makes it possible for one software object that takes on all of the methods and properties of another one. This is often called the is-a relationship. A truck is-a vehicle.A square is-a shape.A dog is-a mammal. The idea is that more general and widely used methods and properties are defined parent classes and these are made more specific in child classes (often called subclasses). Mammal is a more general description than dog. Whales are mammals. The big benefit is that you can organize your code so you only have to write code that does something that lots of objects have to do once in the parent. All employees have to have an employee number assigned to them. More specific code can be part of the child classes. Only employees that work in the general office need to have an employee door card key assigned to them. This new capability of inheritance requires new rules, however. If a new class is based on an old one, Protected is an access modifier that reflects that relationship. Protected code can be accessed only from within the same class, or from a class derived from this class. You dont want employee door card keys being assigned to anyone except employees. As noted, Protected Friend is a combination of the access of both Friend and Protected. Code elements can be accessed either from derived classes or from within the same assembly, or both. Protected Friend can be used to create libraries of classes since code that accesses your code only has to be in the same assembly. But Friend also has that access, so why would you use Protected Friend? The reason is that Friend can be used in a Source file, Namespace, Interface, Module, Class, or Structure. But Protected Friend can only be used in a Class. Protected Friend is what you need for building your own object libraries. Friend is just for difficult code situations where assembly wide access is really required.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.