VB variables, constants and data types and process overview (iii)

Source: Internet
Author: User
Tags constant expression valid

Constant

It is often found that the code contains some constant values that recur over and over again. You can also find that your code uses numbers that are hard to remember, and those numbers don't have a clear meaning.
In these cases, the available constants greatly improve the readability and maintainability of the code. Constants are meaningful names that replace values or strings that never change. Although constants are somewhat like variables, they cannot be modified as constants, nor can new values be assigned to constants. There are two sources of constants:
1. Internal or system-defined constants are provided by applications and controls. Visual Basic constants are enumerated in the Visual Basic (VB), and Visual Basic for Applications (VBA) object libraries in the Object Browser. Other applications that provide object libraries, such as Microsoft Excel and Microsoft Project, also provide a list of constants that can be used with the application's objects, methods, and properties. Constants are also defined in the object library of each ActiveX control. For more information about using the Object Browser, see Chapter Nineth, "Programming with objects."
2. Symbolic or user-defined constants are declared using the Const statement. The following section, "Creating Your Own constants," describes user-defined constants.
In Visual Basic, the name of a constant is a mixed-case format whose prefix represents the object library name that defines the constant. Constants from Visual Basic and Visual Basic for Applications object libraries begin with "VB", such as Vbtilehorizontal.
When designing a prefix, you should try to prevent accidental conflicts, and you cannot have a case where the constant name is the same but represents a different value. Even if you use a prefix, two object libraries may still contain the same constants that represent different values. In this case, the reference to which constant depends on which object library has a higher priority. For information about changing object library precedence, see the "References Dialog" section.
To make absolutely sure that a constant name conflict does not occur, use the following syntax to qualify a reference to a constant:
[Libname.] [ModuleName.] Constname
Libname is typically the class name of a control or library. ModuleName is the name of the module that defines the constants. Constname is the constant number name. Each element is defined in the object library, and the element is viewable in the Object Browser.

The syntax for the

Create your own constants
Declaration constants is:
[public| Private] Const constantname[as type] = expression
Parameter constantname is a valid symbol name (the same rule as the rule that establishes the variable name), expression Consists of numeric constants or string constants and operators, but function calls cannot be used in expression. The
Const statement can represent the number, date, and time:
Const CONPI = 3.14159265358979
Public Const conmaxplanets as Integer = 9
Const Conrele Asedate = #1/1/95#
Also use the Const statement to define a string constant:
Public Const CONVERSION = "07.10.A"
Const CONCODENAME = "Enigma"
If you use a tease Number, you can place multiple constant declarations on one line:
public Const CONPI = 3.14, Conmaxplanets = 9, _
Conworldpop = 6E+09
Equals (=) the expression on the right is often a number or text String, but it can also be an expression whose result is a number or string (although the expression cannot contain a function call). You can even define new constants with previously defined constants.
Const conPi2 = CONPI * 2 Once a constant is defined, it can be placed in code to make the code more readable. For example:
Static solarsystem (1 to conmaxplanets)
If numpeople > Conworldpop Then Exit Sub

Set the scope of user-defined constants
As with variable declarations, the Const statement also has a scope and uses the same rules:
To create a constant that exists only in the procedure, declare the constant inside the procedure.
To create a constant that is valid for all procedures in the module, but not for any code outside the module, declare the constant in the module's declaration section.
To create a constant that is valid throughout the application, declare it in the Declaration section of the standard module and place the Public keyword before the Const. You cannot declare a public constant in a form module or class module.
For more information about scope, see the "Understanding the scope of Variables" section earlier in this chapter.

Avoid circular references
Because constants can be defined with other constants, it is important to be careful not to have loops or circular references between more than two constants. A loop occurs when there are more than two common constants in the program, and each common constant is defined with another. For example:
' In the Module1:
Public Const Cona = conb * 2 ' in the entire application
' In effect.
' In Module 2:
Public Const conb = Cona/2 ' in the entire application
' In effect.
If a loop occurs, Visual Basic generates an error message when you try to run the application. You cannot run a program without resolving a circular reference. To avoid loops, you can limit the public constants to a single module, or at most only a few modules.

A

Data type
variable is the place where the value is stored, and it has a name and a data type. The data type of a variable determines how the bits representing those values are stored in the computer's memory. You can also specify its data type when declaring a variable. All variables have data types to determine what kind of data can be stored. The
by default, if the data type is not specified in the declaration, the data type of the variable is variant. Variant data types are like a chameleon, which can represent different data types on different occasions. When you specify that a variable is a variant, you do not have to convert between data types, and VisualBasic automatically completes all the necessary transformations.
However, if you know that a variable does always store a particular type of data, and you also declare a variable of that particular type, Visual Basic processes the data with greater efficiency. For example, a variable that stores a person's name is best represented as a String data type because the name is always made up of characters.
In addition to variables, data types are also used in other contexts. When you assign a value to a property, the value has a data type, and the parameter of the function has a data type. In fact, in Visual Basic, everything related to data is related to the data type.
You can also declare an array of any base type.
For more detailed information, see the "Arrays" section later in this chapter. The 15th chapter, "Design for performance and Compatibility", also discusses improving application performance by selecting data types.

Declaring a variable with a data type
Before you use a non-Variant variable, you must declare the variable as type with Private, public, Dim, or Static statements. For example, the following statements declare variables of type Integer, Double, String, and Currency, respectively:
Private I as Integer
Dim AMT as Double
Static YourName as String
Public Billspaid as Currency
A declaration statement can combine multiple declarations, look at the following statement:
Private I as integer,amt as Double
Private YourName as string,billspaid as Currency
Private Test,amount,j as Integer
Note If you do not supply a data type, the specified variable is the default type. In the example above, the variable test and Amount are variant data types. If the experience in other programming languages makes you feel that all the variables in the same declaration statement have the same specified data type (Integer in this case), the results here may be refreshing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.