What does DIM mean in Visual Basic and BASIC? Subroutines of the basic language.

Dim had different meanings attributed to him.

I found references to Dim value "Declare in Memory", the more relevant link is the Dim Statement document published by Oracle as part of the Siebel VB language reference. Of course, you can argue that if you don't declare variables in memory, where do you do it? May be "Declare in module" - good alternative, considering how Dim is used.

In my opinion, "Declare in memory" is actually a mnemonic created to make it easier to learn how to use Dim . I see "Declare in Memory" as better meaning because it describes what it does in current versions language, but this is not the correct meaning.

In fact, at the origins Basic Dim used only to declare arrays. No keyword was used for regular variables, instead their type was inferred from their name. For example, if the variable name ends in $ , then it's a string (which is something you might even see in pre-VB6 method names, like Mid$). So you've only used Dim to measure array sizes (note that ReDim resizes arrays).

Does it really matter? I mean this keyword makes sense in an artificial language. It must not be a word in English or any other natural language. So it can only mean what you want, all that matters is that it works.

In any case, this is not entirely true. Because BASIC is part of our culture and understands why it is the way I hope it will help improve our vision of the world.

I sit at my computer with the desire to help preserve this little piece of our culture that seems lost, replaced by our guessing what it was. So I dug up MSDN both current and old CDs from the 1998 version. I also searched the documentation for the old QBasic [should have used DOSBox] and managed to get the Dartmouth manual to see what they say about Dim . To my disappointment, they don't say what Dim stands for, only how it's used.

But before my hope was dimmed, I managed to find this BBC Microcomputer Manual (which is claimed to be from 1984 and I have no doubt about it). The BBC microcomputer used a variant of BASIC called BBC BASIC and it is described in the document. Even though he doesn't say what Dim means, he does say (on page 104):

You can measure N$ to have as many records as you want. For example, DIM N $(1000) will create a string array with space for 1000 different names.

As I said, it does not say that Dim stands for dimension, but it is proof that the association of Dim with Dimension was common at the time of this writing.

Now I got an extra surprise later (on page 208), the heading of the section that describes the DIM keyword (note: this is not listed in the content) says:

DIM array size

So, I didn't get the "Dim stands..." quote, but I think it's clear that any decent person able to read this document would take Dim to mean dimension.

With new hope, I decided to find a way to select Dim . Again, I did not find account on this topic, but I was able to find the definitive quote:

Before you can use an array, you must define it in a DIM (dimension) instruction.

You can find this as part of the True BASIC Online User Guides on the webpage of True BASIC inc, a company founded by Thomas Eugene Kurtz, co-inventor of BASIC.

So, during reuse Dim is short for Dimension and yes. This existed in FORTRAN before, so it's likely that it was chosen by FORTRAN influence as Patrick McDonald said in his answer.

Dim sum as string = "this is not a chinese meal" REM example usage in VB.NET ;)

Declares a variable or array.

If variables are listed separated by commas (for example, DIM sPar1, sPar2, sPar3 AS STRING), only the Variant variable can be defined. AT general case, each variable must be defined on a separate line.

DIM sPar1 AS STRING

DIM sPar2 AS STRING

DIM sPar3 AS STRING

Dim declares local variables in procedures. The PUBLIC and PRIVATE statements are used to declare global variables.

Syntax:

Dim Variable [(Start To End)] [, Variable2 [(Start To End)] [,...]]

Options:

Variable: An arbitrary name for a variable or array.

Start, End: numeric values ​​or constants specifying the number of elements (Number_of_elements=(Start-End)+1) and index range.

Start and End can also be numeric expressions if ReDim is applied at the procedure level.

Type: A keyword that declares the type of the variable.

Keyword: variable type

Bool: Variable of type Boolean (True, False)

Currency: a variable of type Currency (a currency value with a fraction length of 4 positions)

Date: Variable of type Date

Double: double precision floating point variable (1.79769313486232 x 10E308 - 4.94065645841247 x 10E-324)

Integer: variable of type Integer (-32768 - 32767)

long: variable long type Integer (-2 147 483 648 - 2 147 483 647)

Object: variable of type Object (Note: this variable can only be defined later using the Set statement!)

Single: Single type variable (3.402823 x 10E38 - 1.401298 x 10E-45)

String: variable of type String with maximum length 64,000 ASCII characters.

: variable of type Variant (covers all types, as defined). If the keyword is not specified, then the variables are automatically defined as variables of type Variant, except when the operator from the DefBool - DefVar range is used.

In the Basic Lotus® Symphony™ language, variables do not need to be explicitly declared. The only exception is arrays. A variable can be declared using the Dim statement; if there are several declaration statements, they are separated by commas. To declare the type of a variable, specify a type declaration character after its name, or enter the appropriate keyword.

In the Basic Lotus Symphony language, you can define one-dimensional or multidimensional arrays, specifying the appropriate variable type. Arrays are useful for working with lists and tables. Array elements can be accessed by index, which can be specified via a numeric expression or a variable.

Arrays are declared with the Dim operator. An index range can be defined in two ways:

DIM text(20) as String REM 21 elements numbered 0 to 20

DIM text(5 to 25) as String REM 21 elements numbered 5 to 25

DIM text(-15 to 5) as String REM 21 elements (including 0)

REM with numbers -15 to 5

2D data field

DIM text(20,2) as String REM 63 elements numbered 0 to 20 level 1, 0 to 20 level 2, and 0 to 20 level 3

Array types can be declared dynamic if the ReDim statement specifies the number of dimensions in the procedure or function containing the array. In general, the dimension of an array is defined only once and cannot be changed. An array can be declared in a procedure using the ReDim statement. Dimensions can only be defined using numerical expressions. This ensures that the field length restrictions are respected.

Example:

Dim sVar As String

Dim iVar As Integer

sVar = "Lotus Symphony"

REM Two-dimensional data field

Dim stext(20,2) as String

Const sDim as String = " Dimension:"

stext(i,ii) = str(i) & sDim & str(ii)

Dim originally (in BASIC) stood for Dimension, since it was used to determine the dimensions of an array.

(The original implementation of BASIC was Dartmouth BASIC, which evolved from FORTRAN where DIMENSIONS is specified.)

Dim is currently used to define any variable, not just arrays, so its meaning is not intuitive.


2018-06-23 16:01

Dim has had different meanings attributed to it.

I found references to Dim meaning "Declare in Memory", a more relevant reference is the document about Dim Statement published by Oracle as part of the Siebel VB language reference. Of course, you can argue that if you don't declare variables in memory, where do you do it? May be "Declare in a module" is a good alternative considering how Dim is used.

In my opinion, "Declare in Memory" is actually mnemonic, created to make it easier to learn how to use Dim . I see "declare in memory" as the best sense, since it describes what it does in current versions of the language, but it's not the correct sense.

In fact, at the origin of Basic Dim was only used to declare arrays. No keyword was used for regular variables, instead their type was inferred from their name. For example, if a variable name ends with $ then it's a string (which is what you could see even in pre-VB6 method names, like Mid$). So you used Dim only to give dimension to arrays (note that ReDim resizes arrays).

Indeed, It matters? I mean this keyword makes sense in an artificial language. It must not be a word in English or any other natural language. So it can only mean what you want, all that matters is that it works.

In any case, this is not entirely true. Because BASIC is part of our culture and understands why it is the way I hope it will help improve our vision of the world.

I sit from my computer with the desire to help preserve this little piece of our culture that seems lost, replaced by our assumption of what it was. So I dug up MSDN both current and old CDs from the 1998 version. I also looked for documentation for the old QBasic [should have used DOSBox] and managed to get the Dartmouth manual to find out how they say Dim . To my disappointment they don't say what makes Dim stand and only say how it's used.

But before my hope was dimmed, I managed to find this BBC Microcomputer Manual (which claims to be from 1984 and I don't want to doubt it). The BBC microcomputer used a variant of BASIC called BBC BASIC and it is described in the document. Although, he doesn't say what Dim's stand does, he does say (on page 104):

You can measure N$ to have as many records as you want. For example, DIM N $ (1000) will create a string array with space for 1000 different names.

As I said, it doesn't say that Dim means Dimension, but it serves as proof that the association of Dim with Dimension was common at the time of this writing.

Now I got an extra surprise (on page 208), the title of the section that describes the DIM keyword (note: this is not listed in the content) says:

DIM array size

So, I didn't get the "Dim stands..." quote, but I think it's clear that any decent person able to read this document would take Dim to mean dimension.

With new hope, I decided to find Dim was chosen. Again, I didn't find an account on this topic, but I was able to find the definitive quote:

Before you can use an array, you must define it in a DIM (dimension) instruction.

You can find this as part of the True BASIC Online User Guide on the webpage of True BASIC inc, a company founded by Thomas Eugene Kurtz, the co-inventor of BASIC.

So Dim is shorthand for DIMENSION, and yes. This existed in FORTRAN before, so it's likely that it was chosen due to FORTRAN influence as Patrick McDonald said in his answer.

Dim sum as string = "this is not a chinese meal" REM example usage in VB.NET ;)


2017-10-05 01:19

This is shorthand for Dimension, as it was originally used in BASIC to indicate the size of arrays.

DIM - (short for size) determines the size of arrays

Part of the original source code BASIC compiler where it will jump when looking for a DIM command where you can clearly see the original intent for the keyword:

DIM LDA XR01 BACK OFF OBJECT POINTER SUB N3 STA RX01 LDA L 2 GET VARIABLE TO BE DIMENSIONED STA 3 LDA S 3 CAB N36 CHECK FOR $ ARRAY BRU *+7 NOT $ ...

It later came to be used to declare all sorts of variables when the ability to specify the type of variables was added in later versions.


2018-06-23 16:02

Measure a variable, basically you are telling the compiler that you will need a variable of that type at some point.

In VBA, as in any other programming language, variables and constants are used to store any values. As the name implies, variables can change, while constants store fixed values.

For example, the constant Pi stores the value 3.14159265… The number “Pi” will not change during the execution of the program, but it is still more convenient to store such a value as a constant.

At the same time, we can use the variable sVAT_Rate to store the VAT rate on purchased goods. Variable value sVAT_Rate may vary depending on the product purchased.

Data types

All variables and constants are of a particular data type. The table below lists the data types used in VBA with a description and range of possible values:

Data type The size Description Value range
bytes 1 byte Positive integers; often used for binary data 0 to 255
Boolean 2 bytes Can be either True or False true or false
Integer 2 bytes Whole numbers (no fractional part) from -32 768 to +32 767
Long 4 bytes Large integers (no fractional part) from -2 147 483 648 to +2 147 483 647
Single 4 bytes Single precision floating point number from -3.4e38 to +3.4e38
Double 8 bytes Double precision floating point number -1.8e308 to +1.8e308
Currency 8 bytes Floating point number, with a fixed number of decimal places from -922 337 203 685 477.5808 to +922 337 203 685 477.5807
Date 8 bytes Date and time - Data of type Date is represented by a floating point number. The integer part of this number expresses the date, and fraction- time from January 1, 100 to December 31, 9999
Object 4 bytes Object reference Any object reference
String changes Character set. String type may be fixed or variable length. More commonly used with variable length Fixed length - up to approximately 65,500 characters. variable length– up to approximately 2 billion characters
Variant changes May contain a date, a float, or a character string. This type is used in cases where it is not known in advance what type of data will be entered. Number - Double, string - String

Obviously, using the table above and choosing the right data type, you can use memory more economically (for example, choose the data type Integer instead of Long or Single instead of Double). However, when using more compact data types, you need to be careful that your code does not try to fit disproportionately large values ​​into them.

Declaring Variables and Constants

Translator's note: Speaking of variables in VBA, it is worth mentioning one more very important point. If we declare a variable but do not assign any value to it, then it is initialized with a default value:
text strings– are initialized blank lines;
numbers - value 0;
type variables Boolean– False;
the dates are December 30, 1899.

Before a variable or constant can be used, it must be declared. To do this, add the following to the macro: simple string code:

Dim Variable_name As Data_Type

In the above line of code Variable_name is the name of the variable that will be used in the code, and Data_Type- this is one of the data types from the table given a little earlier in this article. For example:

Dim sVAT_Rate As Single Dim i As Integer

Constants are declared similarly, but when declaring constants, their value must be immediately indicated. For example, like this:

Const iMaxCount = 5000 Const iMaxScore = 100

It is not necessary to declare variables in Excel. By default, all entered but not declared variables in Excel will have the type Variant and will be able to accept both numeric and text value.

Thus, the programmer can use the new variable at any time (even if it has not been declared), and Excel will treat it as a variable of type Variant. However, there are several reasons why this should not be done:

  1. Memory usage and computational speed. If you do not declare a variable with an indication of the data type, then by default it will be set to the type Variant. This data type uses more memory, than other data types. It would seem that a few extra bytes per variable - not so much, but in practice in created programs there can be thousands of variables (especially when dealing with arrays). Therefore, the extra memory used by variables like Variant, compared to variables of type Integer or Single, can add up to a significant amount. In addition, operations with variables of type Variant are executed much more slowly than with variables of other types, respectively, an extra thousand type variables Variant can significantly slow down calculations.
  2. Prevention of typos in variable names. If all variables are declared, then you can use VBA statementOption Explicit(we will talk about it later) in order to identify all undeclared variables. This eliminates the appearance of an error in the program as a result of an incorrectly written variable name. For example, using a variable named sVAT_Rate, you can make a typo and, assigning a value to this variable, write: “VATRate = 0.175”. It is expected that from now on, the variable sVAT_Rate should contain the value 0.175 - but of course it doesn't. If the mode of mandatory declaration of all used variables is enabled, then the VBA compiler will immediately indicate an error, since it will not find the variable VATRate among those announced.
  3. Highlighting values ​​that do not match the declared type of a variable. If you declare a variable certain type and try to assign data of a different type to it, then an error will appear, without correcting which, you can get a program crash. At first glance, this may seem like a good reason not to declare variables, but in fact, the sooner it turns out that one of variables received not the data that was supposed to receive - so much the better! Otherwise, if the program continues, the results may be incorrect and unexpected, and it will be much more difficult to find the cause of the errors. It is also possible that the macro will be “successfully” executed. As a result, the error will go unnoticed and work will continue with incorrect data!

In this regard, it is desirable to detect an incorrect data type and correct such errors in the code as early as possible. For these reasons, when writing VBA macro it is recommended to declare all variables.

Option Explicit

Operator Option Explicit causes all variables to be used in VBA code to be declared, and flags all undeclared variables as errors during compilation (before code execution starts). Applying this operator is not difficult - just write this line at the very top of the VBA file:

Option Explicit

If you want to always insert Option Explicit to the top of each new VBA module created, this can be done automatically. To do this, you need to enable the option Require Variable Declaration in the VBA editor settings.

This is done like this:

  • In the editor menu Visual Basic click Tools > Options
  • In the dialog that appears, open the tab Editor
  • Check the box Require Variable Declaration and press OK

When enabled, the string Option Explicit will be automatically inserted at the beginning of each new module created.

Scope of Variables and Constants

Each declared variable or constant has its own limited scope, that is, a limited part of the program in which this variable exists. The scope depends on where the declaration of the variable or constant was made. Take, for example, the variable sVAT_Rate, which is used in the function Total_Cost. The following table discusses two options for variable scoping sVAT_Rate declared in two different positions in the module:

Option Explicit Dim sVAT_Rate As Single Function Total_Cost() As Double ... End Function

If the variable sVAT_Rate declared at the very beginning of the module, then the scope of this variable will be the entire module (i.e. the variable sVAT_Rate will be recognized by all procedures in this module).

Therefore, if in the function Total_Cost variable sVAT_Rate will be assigned some value, then next function, executed within the same module, will use the variable sVAT_Rate with the same meaning.

However, if some function located in another module is called, then for it the variable sVAT_Rate will not be known.

Option Explicit Function Total_Cost() As Double Dim sVAT_Rate As Single ... End Function

If the variable sVAT_Rate declared at the beginning of the function Total_Cost, then its scope will be limited to this function only (i.e. within the function Total_Cost, you can use the variable sVAT_Rate, but not outside).

When trying to use sVAT_Rate in another procedure, the VBA compiler will report an error because this variable was not declared outside of the function Total_Cost(provided that the operator is used Option Explicit).

In the example shown above, the variable is declared at the module level with the keyword Dim. However, it may be necessary that the declared variables can be used in other modules. In such cases, to declare a variable instead of a keyword Dim keyword must be used Public.

By the way, in order to declare a variable at the module level, instead of the keyword Dim keyword can be used Private, which indicates that this variable is intended for use only in the current module.

You can also use keywords to declare constants. Public and Private, but not instead of the keyword Const, along with it.

The following examples show the use keywords Public and Private applied to variables and constants.