Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so:

  • Public - visible to all code inside the module and all code outside the module, essentially making it global.
  • Private - visible only to code inside the module.

I've noticed that you can use Dim and Global as modifiers for modular variables. Are Dim and Global different from Private and Public, respectively, when used as access modifiers on modular fields? If so, how are they different?

like image 691
Ben McCormack Avatar asked Sep 28 '10 17:09

Ben McCormack


People also ask

What is the difference between dim and public?

A: Dim and Public variables are temporary holding locations for values that normally vary during program execution. These values are typically the result of measurements and processing or calculations. The main difference between a Dim and a Public variable is that Dim variables do not show up in the Public table.

Is Dim public or private?

Module-level variables can be either public or private. Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module. By default, variables declared with the Dim statement in the Declarations section are scoped as private.

Where there is no difference between dim and private in VBA?

Explanation: There is no different between using Dim or Private on a module/class level variable, because they mean the same thing. Inside an actual code block or method, you can't use the private keyword.


1 Answers

Dim and Private work the same, though the common convention is to use Private at the module level, and Dim at the Sub/Function level. Public and Global are nearly identical in their function, however Global can only be used in standard modules, whereas Public can be used in all contexts (modules, classes, controls, forms etc.) Global comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public.

like image 93
Joe Jordan Avatar answered Oct 11 '22 05:10

Joe Jordan