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

Source: Internet
Author: User
Tags integer modify

Process overview

Dividing a program into smaller logical parts simplifies program design tasks. These parts are called processes, and they can become artifacts that enhance and extend Visual Basic. Procedures can be used to compress repetitive tasks or share tasks, such as frequently compressed computations, text and control operations, and database operations.
There are two great benefits to programming with process:
1. Process can divide the program into discrete logical units, each unit is easier to debug than the whole program without process.
2. A process in a procedure, often without modification or with only minor changes, can become a component of another program.
Use the following procedures in Visual Basic:
A Sub procedure does not return a value.
Function procedure returns a value.
The Property procedure returns and specifies a value and sets the object reference.
For more information about property procedures, see Chapter Nineth, "Programming with objects."

Sub procedure
A child procedure is a block of code that executes when responding to an event. After the code in the module is divided into child procedures, it becomes easier to find and modify the code in the application.
The syntax for a child procedure is:
[private| Public][static]sub procedurename (arguments)
Statements
End Sub
Each call procedure executes the statements between the sub and End Sub. You can put child procedures in standard modules, class modules, and form modules. By default, the child procedures in all modules are public (common), which means that they can be invoked anywhere in the application.
The arguments of a procedure is similar to a variable declaration, which declares a value that is passed in from the calling procedure. In Visual Basic, you should distinguish between common procedures and event procedures.

General Procedures
A common procedure tells the application how to accomplish a specified task. Once a common procedure is identified, it must be invoked exclusively by the application. Conversely, the event procedure is always idle until an event procedure is invoked in response to an event raised by a user or a system-raised event.
Why build a common process? One reason is that several different event processes may have to perform the same action. It is the best programming policy to put a common statement into a separate process (a common procedure) and invoke it by an event procedure. This eliminates the need to duplicate the code and easily maintain the application. For example, the VCR sample application uses a common procedure, and the Click events for several different scrolling buttons call this generic procedure. Figure 5.7 illustrates the use of common procedures. The code in the Click event invokes the child procedure of the Button manager, which runs its own code, and then returns control to the Click event procedure.

event procedure when an object in Visual Basic determines the occurrence of an event, it automatically invokes the event's procedure with the appropriate name of the event. Because the name establishes a connection between the object and the code, the event procedure is attached to forms and controls.
The event procedure for a control combines the actual name, underscore (_), and event name of the control (as specified in the Name property). For example, if you want the button to invoke an event procedure after you click a command button named Cmdplay, use the Cmdplay_click procedure.
A form event procedure combines the word "form", the underscore, and the event name. If you want the window to invoke an event procedure after you click the form, use the Form_Click procedure. (As with controls, forms have unique names, but they cannot be used in the name of the event procedure.) If you are using an MDI form, the event procedure combines the words "MDIForm", underscores, and event names, such as Mdiform_load.
All event procedures use the same syntax.

Although you can write event procedures yourself, it is more convenient to use the code procedure provided by Visual Basic, which automatically includes the correct procedure name. Select an object from the object frame and select a procedure from the procedure box to select a template in the Code Editor window.
It is a good idea to set the Name property of a control before you start writing an event procedure for the control. If you attach a procedure to a control and then change the name of the control, you must also change the name of the procedure to conform to the new name of the control. Otherwise, Visual Basic cannot match the control to the procedure. When a procedure name does not match the name of the control, the procedure becomes a generic procedure.
Details Visual Basic identifies various events for various forms and controls. See the language reference for a description of all events.

Function Procedure
Visual Basic contains built-in or internal functions, such as SQR, Cos, or CHR. In addition, you can use function statements to write your own function procedures. The syntax for the
Function procedure is:
private| Public][static]function procedurename (arguments) [as type]
Statements
End Function
As with Sub procedure, Function A procedure is also a separate process that reads parameters, executes a series of statements, and changes the values of its parameters. Unlike a child procedure, a Function procedure can return a value to the calling procedure. There is a three-point difference between a Sub procedure and a Function procedure:
Generally, a Function procedure name and parameter (returnvalue = function) is called to the right of a larger statement or expression. The
is exactly the same as a variable, and the Function procedure has a data type. This determines the type of return value (if there is no as clause, the default data type is Variant).
to assign a value to the procedurename itself, you can return this value. When a Function procedure returns a value, the value can become part of a larger expression.
For example, the following is a value known as the two right angles of a right triangle, which computes a third side (bevel) function:
function hypotenuse (A As Integer, B as Integer) as String
Hypotenuse = SQR (A ^ 2 + B ^ 2
End Function
The method that calls a Function procedure in Visual Basic is the same as the method that calls any intrinsic function:
Label1.Caption = hypotenuse (CInt (Te XT1. Text), _
CInt (text2.text))
Strx = hypotenuse (Width, Height)
For more information about function procedures, see "In the Visual Basic 6.0 Language Reference manual" Function statement ". The techniques for invoking various types of procedures are discussed in detail in the "Calling procedures" section later in this chapter.

Use process
Create a new procedure
To create a new generic procedure,
Enter the procedure header in the Code window and press ENTER. The procedure head begins with a Sub or Function, followed by a name. For example, you can enter in any of the following ways:
Sub UpdateForm ()
Function Getcoord ()
Visual Basic responds by completing the template for the new process.

Select an existing procedure
To view the procedure in the current module,
To view an existing generic procedure, select General in the object frame of the Code window, and then select the procedure in the procedure box.
Or
To view the event procedure, select the appropriate object in the Object box in the Code window, and then select the event in the procedure box.
To view procedures in other modules,
1. Choose Object Browser from the View menu.
2. Select the project in the Project/library box.
3. Select the module in the class/Module list and select the procedure in the members list.
4. Select "View definition".

Call procedure
There are a number of techniques for calling procedures that are related to the type and location of the procedure and how it is used in the application. The following sections describe how to call Sub procedures and Function procedures.

Call Sub procedure
Unlike a Sub procedure, in an expression, a Sub procedure cannot be invoked with its name. The call to the Sub procedure is a separate statement. A Sub procedure is not the same as a function, it does not return a value by name. However, as with function procedures, Sub procedures can also modify the values of any variables passed to them.
There are two ways of calling a Sub procedure:
' The following two statements call a Sub procedure named MyProc.
Call MyProc (firstargument, secondargument)
MyProc firstargument, Secondargument
Note that when you use the call syntax, the arguments must be in parentheses. If you omit the call keyword, you must also omit the parentheses on either side of the argument.

Calling a Function procedure
Typically, the method of invoking a Function procedure that is written by itself is the same as a method that invokes a Visual Basic internal function procedure, such as Abs, that is, to write its name in an expression.
' The following statements call function Todec.
Print * Todec
X = Todec
If Todec = Then Debug.Print "Out of Range"
X = Anotherfunction (Ten * Todec)
You can call a function as well as a Sub procedure. The following statements call the same function:
Call year [now]
When you call a function in this way, Visual Basic discards the return value.

Calling a procedure in another module
Common procedures in other modules can be invoked anywhere in the project. You may need to specify such a module that contains the procedure that is being invoked. The techniques for invoking procedures in other modules depend on whether the procedure is in a form module, in a class module, or in a standard module.

Procedures in a form
External calls to all form modules must point to the form module that contains the procedure. If you include a somesub procedure in the form module Form1, you can use the following statement to invoke the procedure in Form1:
Call Form1.somesub (arguments)

Procedures in a class module
Like a call procedure in a form, calling a procedure in a class module invokes a variable that is consistent with the procedure and points to a class instance. For example, DemoClass is an instance of class Class1:
Dim DemoClass as New Class1
Democlass.somesub
However, unlike the form, you cannot use the class Masterpiece qualifier when referencing an instance of a class. You must first declare an instance of the class to be an object variable (DemoClass in this case) and refer to it with the variable name.
Details about object variables and class modules can be found in chapter Nineth, "Programming with objects."

Related Article

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.