Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
You control the availability of a variable by specifying its access level. The access level determines what code has permission to read or write to the variable.
Member variables (defined at module level and outside any procedure) default to public access, which means any code that can see them can access them. You can change this by specifying an access modifier.
Local variables (defined inside a procedure) nominally have public access, although only code within their procedure can access them. You cannot change the access level of a local variable, but you can change the access level of the procedure that contains it.
For more information, see Access levels in Visual Basic.
Place the Dim Statement for the variable inside the module, class, or structure, but outside any procedure.
Include the Private keyword in the Dim
statement.
You can read or write to the variable from anywhere within the module, class, or structure, but not from outside it.
For a member variable, place the Dim
statement for the variable inside a module, class, or structure, but outside any procedure.
Include the Public keyword in the Dim
statement.
You can read or write to the variable from any code that interoperates with your assembly.
-or-
For a local variable, place the Dim
statement for the variable inside a procedure.
Do not include the Public
keyword in the Dim
statement.
You can read or write to the variable from anywhere within the procedure, but not from outside it.
You can limit the access level of a variable to its class and any derived classes, or to its assembly. You can also specify the union of these limitations, which allows access from code in any derived class or in any other place in the same assembly. You specify this union by combining the Protected
and Friend
keywords in the same declaration.
Place the Dim
statement for the variable inside a class, but outside any procedure.
Include the Protected keyword in the Dim
statement.
You can read or write to the variable from anywhere within the class, as well as from within any class derived from it, but not from outside any class in the derivation chain.
Place the Dim
statement for the variable inside a module, class, or structure, but outside any procedure.
Include the Friend keyword in the Dim
statement.
You can read or write to the variable from anywhere within the module, class, or structure, as well as from any code in the same assembly, but not from outside the assembly.
The following example shows declarations of variables with Public
, Protected
, Friend
, Protected Friend
, and Private
access levels. Note that when the Dim
statement specifies an access level, you do not need to include the Dim
keyword.
Public Class classForEverybody
Protected Class classForMyHeirs
Friend stringForThisProject As String
Protected Friend stringForProjectAndHeirs As String
Private numberForMeOnly As Integer
The more restrictive the access level of a variable, the smaller the chances that malicious code can make improper use of it.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Control variable scope and logic using code blocks in C# - Training
Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.