| Base1_Ball | Approaches |
| 1Base_Ball | Does not approach, as begins not with a letter. |
| Base.1 | Does not approach, as the point is used. |
| Base&1 | Does not approach, as it is used амперсанд. |
| ThisIsLongButOK | Does not approach, as length of only 15 characters. |
Visual Basic works with 14 standard types of variables. Besides, the programmer can define own data type. In the given section those from them which in the core are applied by operation dataful are described.
String
String variables store characters. One of methods to designate the given variable type consists in "$ character adding by her name end, for example: AStringVariable $. Theoretically such variable can contain to 2 billion characters. Anyway on the specific computer this number will be much less because of limited scopes of a random access memory, resources Windows or number of the characters used in shape.
One of the most main applications of string variables consists in a choice of the information of data entry fields. For example, if there is a data entry field with name Textl, then operator ContentOfTextlS = Textl. Text appropriates a line from a variable data entry field in the left part of the given operator.
Integer
Integer variables can store only rather small integer numbers (in a range from-32768 to +32767). Arithmetical operations over such numbers are fulfilled very quickly. For a designation of this kind the character of "%" is used:
AnIntegerVariable % = 3
Long Integer
The given type appeared for the first time in language QuickBASIC. The whole values are stored In such variables from-2 147483648 to +2 147483647. It is designated by the character "and". Arithmetical operations over such numbers are fulfilled too very quickly, and by operation with the processor 386DX or 486DX there is only small difference in speed of calculations between Long Integer and Integer.
ALongIntegerVariableS = 123456789 Single Precision
The identifier for the given numbers is the character "!". The given variable type allows to store fractional numbers to within seven digits. It means that if the result 12345678.97 the part 8.97 is not exact turns out. The result can be equal, for example, 12345670.01. The length of numbers can reach 38 signs. Calculations by dataful variables too will be approximate. Moreover, arithmetical actions are fulfilled more slowly, than with integer variables.
Double Precision
Variables of this kind allow to store numbers to within 16 digits and length to 300 characters. The identifier is "#". Calculations with them too are approximate, and their speed rather small. In the core variables of type Double Precision are used for scientific computations.
Currency
The given type missed in versions GW-BASIC and QuickBASIC. It is created to avoid errors at conversion of decimal numbers to binary form and on the contrary. (It is impossible 1/10 to present as the total 1/2,1/4, 1/8 1/16, etc.) the Given type can have to 4 digits after a comma, and to 14 - before it. In the given range of calculation will be exact. The identifier represents character. As all arithmetical operations, except addition and subtraction, are fulfilled as slowly, as well as in case of variables with the double precision, the given type is preferable to carrying out of financial calculations. (For those who is interested: the given type uses integer numbers from 19 discharges which then share on 10000. It allows to organize 15 signs to a comma and 4 after it.)
Date
This data type allows to store values of time and date in an interval from midnight on January, 1st 100 years till midnight on December, 31st 9999 years. Such values in the text of programs are designated by characters "#", for example:
Millenium = #January 1, 2000#
If value of date is entered only, Visual Basic supposes that time equals 00:00.
Byte
The byte type is new in Visual Basic 5 and is intended for storage of integer numbers from 0 to 255. Its usage allows to save strongly a random access memory and to reduce the size of arrays (see chapter 10) in comparison with prior versions Visual Basic. Besides, it is used by operation with binary files.
Boolean
The boolean data type can store only two values: True or False. Its usage instead of integer variables is good programming style.
Variant
The given type is added in Visual Basic 5 of version 2.0. The type variable variant can contain the data of any type. If Visual Basic does not know type of the accepted data, it is necessary to use variant.
The information type has no value as variant can contain any data type (numerical, date / time, string). Visual Basic automatically produces necessary data transformations, therefore it is not necessary to worry about it. On the other hand, it is possible to use intrinsic functions for check of the data type, stored in a type variable variant. They allow to check up easily, whether correctly the user enters the information.
Usage of the data of this kind as variant, decelerates program operation as time and resources for type conversion operations is required. Besides, many programmers understand that usage of automatic type conversions of the data leads to an inaccurate type of programs. The unique reason in usage variant consists in possible errors at type conversion by immediately programmer.
More in detail about variables
Unlike the majority of version BASIC, in one program Visual Basic it is impossible to use the names of variables different only by type (identifier), for example, And % and And!. At attempt to use a double name there is an error "double determination" (duplicate difmition) at program start.
At the first usage of a variable Visual Basic temporarily appropriates to it empty value and type variant. This value disappears while a variable appropriate the real. The sample of "empty" value has each data type. For string variables it is a line of zero length (""). For numerical variables it is a zero. It is possible to rely only on values by default if they are documented (for example, in comments). Otherwise it is necessary to face set of difficultly audible errors. Therefore it is recommended to initialize values of variables in the first lines of event procedures.
Example: an exchange of values
One of widespread tasks is the exchange of values between two variables. Surprisingly, but developers Visual Basic removed operator Swap known in QuickBASIC from language. Therefore it is necessary to write the code. We assume that there are two variables х and at, and try to change places of their value:
х = at
At = х
Such variant will not work, but it is important to understand because of what. The first operator appropriates a variable х variable value at, but that самымуничтожает old value х. As a result of the second the operator appropriates a variable at its initiating value. The decision consists in temporary variable usage:
temp = х ' Copies old value х in temp
х = at ' Now х contains old value at
At = temp ' Now at contains original value х
The declaration of variables: operator Dim for various data types
The majority of people prefer not to use identifiers for a variable type designation (as for such types as date/time, does not exist at all identifiers). Instead they use the operator Dim. Such operation carries a declaration title. The declaration of types of variables in event procedures before their usage - and, naturally, with comments - is good style in programming. It also helps to refine читабельность texts of programs.
Private Sub cmdCalculate Click
' the Given procedure calculates percent on the mortgage
Dim Years As Integer
Dim Rate As Currency
Dim Amount As Currency
Dim I As Integer
Dim TextBox As String
Dim Interest As Currency End Sub
Declarations of variables can be combined in single line, for example:
Dim Year As Integer, Rate As Currency, Name As String
Dim X, Y, Z As Integer
In the assumption that all three variables will be the whole type. Actually in this case variables Х and Y will be declared as variant, and only Z - as integer. To avoid it, it is necessary to use each time the type identifier.
If the variable is declared by means of operator Dim, in case of usage of a variable to the same name and other identifier of type there will be an error "double determination" at program start. For example, if the following expression Dim Count As Integer declares variable Count it is impossible to use variables CountS, Count!, Count# and Count@. It is possible to use only name Count of %, but it only other form for a name of variable Count.
And, at last, to appropriate a variable type variant, use the operator Dim without As:
Dim Foo ' declares Foo a type variable variant
It is possible to write and so:
Dim Foo As Variant ' is easier for perusal
Necessity of the declaration of variables
One of the most general errors in programs is usage of not declared variables. The problem consists that Visual Basic allows to create variables "on the fly" as soon as they the first time meet in the program. If to confuse a variable name. Visual Basic creates new with a similar name and appropriates to it value by default that there and then leads to errors. This error is difficult for finding out, as names of variables are very similar.
The elementary method to avoid similar errors consists in the forced declaration of all variables. Then at an error in a variable name the message will be produced. Developers Visual Basic provided such possibility of operation, but by default it is not used.
The operator necessary for it, is called Option Explicit. It is the first of the operators, not used in event procedures. A good method to remember for itself its principle of operation consists in usage Option Explicit for change of values by default.
To place Option Explicit in section (General), it is necessary:
(For copying of the sample program from reference system it is possible to use button Copy in window Code for an example. Then it is possible to use point Paste from menu Edit for an example insertion in window Code.). After Visual Basic meets command Option Explicit, he more does not allow to use not declared variables. If to try to apply nevertheless such variable, the error report, as shown will be produced more low.

Change of values by default for types
Following expression automatically appropriates to variables I, J and To type variant:
Dim I, J, TO
Let's assume, in the new program almost all variables should be integer. In that case it is convenient to organize their declaration so that the variable for which the type is not specified, was not declared any more as variant. Operator DefType is for this purpose used. In the following table some examples of its usage are resulted:
|
|
|
|
|
All variables receive type integer. |
|
|
All variables beginning with letters I and J, receive type integer. |
|
|
All variables beginning with letters from S to Z, receive type String |
Deflnt a range of letters (for integer)
DefLng a range of letters (for long integer)
DefSng a range of letters (for single precision)
DefDbl a range of letters (for double precision)
DefCur a range of letters (for currency)
DefStr a range of letters (for string)
DefVar a range of letters (for variant)
DefBooi a range of letters (for boolean)
DefByte a range of letters (for byte)
DefDate a range of letters (for date)
Letters should be not mandatory the capital: DefStr s-Z and DefStr S-Z work equally. Agreements on types can be changed always, using the identifier or operator Dim for a specific variable. The operator DefType allocate in the same section (General), as Option Explicit.
Area of visibility of variables
Programmers apply the term visibility area when want to tell about possibility of usage of a variable from one part of the program in its other part. In old programming languages all variables were accessible in all parts of the program, therefore saving of integrity of names represented the big problem. For example, if in application two variables Total their values destroyed each other were used.
The decision of the given problem in the modern languages of high level, such as Visual Basic, consists in isolation of variables in procedures. Until then while it is not set in special way, variable value Total in one procedure does not influence in any way variable value with the same name in other procedure. In programming terms the given approach means that the given variables are local in relation to procedures if it is not specified differently. In particular, event processing procedure normally has no communication with other procedure of the same type. It is normally not accepted to work with variables by default. If it is necessary to be assured that the given variable is local, it is necessary to declare it in procedure of event by means of operator Dim.