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

Source: Internet
Author: User
Tags abs execution valid

When you perform calculations in a Visual Basic environment, you often need to store data temporarily. For example, you might want to calculate several values, compare them, and manipulate them differently based on the results of the comparisons. If you want to compare these values, you store them, but you do not have to save them to a property. Like most programming languages, Visual Basic uses variables to store values. A variable has a name (a noun used to refer to the value contained in the variable) and a data type (determines the kind of data that the variable can store). Arrays can be used to store the set of related variables that have an index set.
Constants also store values, as the name suggests, and the values remain unchanged throughout the execution of the application. The use of constants increases the readability of the code, because we see names that have meaning rather than numbers. Visual Basic has many internal constants, but you can also establish custom constants.
In Visual Basic, data types control the internal storage of data. By default, VisualBasic uses Variant data types. There are many other available data types that can be used to optimize the speed and size of your code when you do not need the flexibility provided by a Variant.

Variable
The value is temporarily stored with a variable during application execution in Visual Basic. A variable has a name (a word used to refer to the value contained in a variable) and a data type (determines the kind of data that a variable can store).
You can think of a variable as the place where the unknown value resides in memory. For example, suppose you are making a software that sells apples to a fruit shop. Apple's price and sales were not known until the sale actually took place. At this point, you can design two variables to hold the unknowns and name them Appleprice and ApplesSold. Each time the program is run, the user provides a specific value for the two variables. In order to calculate the total sales and display the results in a text box called TxtSales, the code should be like this:
TxtSales.txt = Appleprice * applessold
This expression returns a different amount each time, based on the value provided by the user. Because you have a variable, you can design an equation without having to know what the actual input is.
In this example, the Appleprice data type is currency, and the ApplesSold data type is an integer. Variables can also represent many other values, such as text values, dates, various numeric types, and even objects in this column.

Storing and retrieving data in a variable
Evaluates with an assignment statement and assigns the result to a variable:
ApplesSold = 10 ' will be worth 10
' Pass to the variable.
ApplesSold = applessold + 1
' Variable value
' Add one.
Note that the equals sign in the example is an assignment, not equal to an operator; it assigns a value (10) to a variable (applessold).

declaring variables
Declaring a variable is to notify the program beforehand of the variable. To declare a variable with the Dim statement, the Dim statement provides the variable name:
Dim variablename [as type]
Variables declared within a procedure with a Dim statement exist only if the procedure is executed. The value of the variable disappears as soon as the process is finished. In addition, the value of a variable in a procedure is local to a procedure, that is, a variable in another procedure cannot be accessed in one procedure. Because of these features, you can use the same variable names in different processes without worrying about conflicts and unexpected changes.
Variable name:
1. Must begin with a letter.
2. Cannot contain an embedded (English) period or an embedded type declaration character.
3. No more than 255 characters.
4. It must be unique within the same context. Scope is a variable field that can reference variables, such as a procedure, a form, and so on.
Because of the optional as type clause in the Dim statement, you can define the data type or object type of the variable being declared. The data type defines the type of information that the variable stores. String, integer, and currency are examples of data types. Variables can also contain objects from Visual Basic or other applications. Object, Form1, and TextBox are instances of Visual Basic object types or classes.
For more information about objects, see Chapter Nineth, "Programming with Objects" and chapter tenth, "Programming with parts." Data types are discussed in detail in the "Data Types" section later in this chapter. Other ways to declare variables:
is not inside the procedure, but declares the variable in the declaration segment of the form, standard, or class module, which makes the variable valid for all procedures in the module.
Declaring a variable with the Public keyword will make the variable valid throughout the application.
Declare a local variable with the static keyword, and the value of the variable is retained even if the procedure ends.

Implicit declarations
do not have to declare this variable before using a variable. For example, you can write a function in which you do not have to precede the variable tempval with the
clear it:
Function safesqr (num)
TempVal = Abs (num)
Safesqr = SQR (TEMPV AL)
End Function
Visual Basic creates a variable automatically with this name, and it can be considered explicitly declared when using this variable. Although this method is convenient, if the variable name misspelled, it will cause a difficult to find error. For example, suppose you write a function like this:
function safesqr (num)
TempVal = Abs (num)
Safesqr = SQR (temval)
End Function
At first glance, these two paragraphs The code seems to be the same. But because the TempVal variable name is written incorrectly in the penultimate line, the function always returns 0. When Visual Basic encounters a new name, it cannot tell whether this means implicitly declaring a new variable, or simply writing an existing variable name incorrectly, and then having to create a new variable with that name.

Explicit declaration
to avoid the trouble of writing a variable name incorrectly, you can specify that Visual Basic emits an error warning whenever you encounter a name that is not explicitly declared as a variable. To explicitly declare a variable, please
include this statement in the declaration section of a class module, form module, or standard module:
option Explicit
-or-
on the Tools menu, click the Editor tab, and then check the request variable declaration option. This automatically inserts the option Explicit statement into any new module, but does not automatically insert in the already established module, so within the project, you can only manually add option Explicit to an existing module.
If you execute the statement on a form or standard module that contains a SAFESQR function, Visual Basic determines that both tempval and temval are undeclared variables, and that you send an error message for both. You can then explicitly declare tempval.
Function safesqr (num)
Dim tempval
TempVal = Abs (num)
Safesqr = SQR (temval)
End Function
because Visu Al Basic displays an error message to the misspelled temval, so you can immediately understand what the problem is. Because the Option Explicit statement helps catch these types of errors, it is best to use it in all code.
Note the scope of the option Explicit statement is limited to the module in which the statement resides, so for each form module, standard module, and class module that requires Visual Basic to force an explicit variable declaration, the option Explicit statement must be placed in the declaration section of those modules. If you select Require variable declaration, Visual Basic automatically inserts Option Explicit in subsequent form modules, standard modules, and class modules, but does not add it to existing code. The Option Explicit statement must be added to any existing module manually in the project.

Understand the scope of a variable
The scope of the variable determines the part of the code that can know the existence of the variable. When declaring a variable inside a procedure, only the code within the procedure can access or change the value of that variable; it has a scope that is local to the process. However, there are times when you need to use a variable with a larger scope, such as a variable whose value is valid for all processes within the same module, even for all processes in the entire application. Visual Basic allows you to specify its scope when declaring a variable.

Specify a valid range for a variable
Whether a variable is scoped is considered a procedure level (local) variable or a module-level variable, depending on how the variable is declared.

Variables used internally in the procedure
Process-level variables are recognized only in the process of declaring them, and they are called local variables. Use Dim or static keyword to declare them. For example:
Dim IntTemp as Integer
Or
Static Intpermanent as Integer
When the entire application runs, the value in the local variable declared with static persists, while the variable declared with Dim only exists during the execution of the procedure.
Local variables are the best choice for any temporary calculation. For example, you can create 10 or so different processes, each containing a variable called inttemp. As long as each inttemp is declared as a local variable, each process only recognizes its own inttemp version. Any process can change the value of its own local inttemp variable, without affecting the inttemp variable in the other process.

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.