Learn to program in Visual Basic .NET, the right way

Methods - Modifiers

What is a Modifier?

Modifiers are permission levels given to things like variables, methods, etc. that determine their respective scope.

What is Scope?

Take for example the following code:

Fiddle: Live Demo

In this example there are 3 levels of scope. They are, from highest to lowest:

  1. Module
  2. Method
  3. Loop

From the module level scope, we cannot access the method level scope nor can we access the loop level scope. The reason is because at the module level, the other scopes do not exist.

From the loop level scope, we can access the method level scope and the module level scope. The reason is because at the lowest level, all of the other scopes do exist.

Private

The Private modifier is the least permissive access level. Private members are accessible only within the body of the class, module, etc. that they reside in.

Private variables or methods can only exist outside of methods. For example, you cannot have a private variable inside of a Sub:

Fiddle: Live Demo

Public

The Public modifier is the most permissive access level. There are no restrictions on accessing public members from anywhere.

Just like the Private modifier, Public variables or methods can only exist outside of methods.

Remarks

It is important to have proper scope management. If you have a Public method that does not need to be accessed outside of it's Module or Class, then change it to Private.