0% found this document useful (0 votes)
9 views2 pages

VBNet Access Modifiers

This document provides a comprehensive guide on VB.NET access modifiers, detailing their visibility and scope across different contexts such as same class, derived class, same assembly, and external assembly. It includes a comparison table of modifiers like Public, Private, Protected, Friend, Protected Friend, and Private Protected, along with their descriptions and key rules for usage. The document emphasizes best practices for using access modifiers to ensure code security and maintainability.

Uploaded by

ashishraj7528
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

VBNet Access Modifiers

This document provides a comprehensive guide on VB.NET access modifiers, detailing their visibility and scope across different contexts such as same class, derived class, same assembly, and external assembly. It includes a comparison table of modifiers like Public, Private, Protected, Friend, Protected Friend, and Private Protected, along with their descriptions and key rules for usage. The document emphasizes best practices for using access modifiers to ensure code security and maintainability.

Uploaded by

ashishraj7528
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VB.

NET Access Modifiers


Visibility & Scope Reference Guide

Access Modifier Comparison Table


Modifier Same Class Derived Class Same Assembly External Assembly

Public ✔ ✔ ✔ ✔
Protected Friend ✔ ✔ ✔ ■ derived

Friend ✔ ✔ ✔ ✘
Protected ✔ ✔ ✘ ✘
Private Protected ✔ ■ same asm ✘ ✘
Private ✔ ✘ ✘ ✘

Scope Hierarchy Diagram


External Assembly (other projects) Public

Same Assembly (same project) Friend Protected Friend

Derived Class (inheritance) Protected Protected Friend Private Protected*

Same Class (current type) Private (all modifiers)

* Private Protected available from VB 15.5+

Modifier Descriptions
Accessible from anywhere — same class, same assembly, or external assemblies. Public
Public
Class MyClass Public Name As String End Class

Accessible only within the same class or structure. Most restrictive modifier. Private balance
Private
As Decimal

Accessible within the same class and all derived (child) classes, regardless of assembly.
Protected
Protected Sub Breathe() ... End Sub

Accessible within the same assembly (project). Equivalent to internal in C#. Friend Class
Friend
HelperClass ... End Class

Accessible within the same assembly OR from derived classes in any assembly. Protected
Protected Friend
Friend Sub SharedMethod()
Accessible only within derived classes in the same assembly. More restrictive than
Private Protected
Protected Friend. Private Protected Sub RestrictedMethod()

Key Rules
• Classes default to Friend if no modifier is specified.
• Class members default to Public if no modifier is specified inside a Public class.
• Private is the recommended default for fields — expose them via Properties.
• Use the most restrictive modifier that still allows your code to work (principle of least privilege).
• Private Protected is available from [Link] 15.5 (Visual Studio 2017 v15.5+).

[Link] Access Modifiers Reference · Generated by Claude

You might also like